Always use the same fill colors barplot ggplot2 in R [duplicate]











up vote
2
down vote

favorite













This question already has an answer here:




  • ggplot2: How to use same colors in different plots for same factor

    3 answers




For different products I am trying to plot the sales per saleschannel in a stacked barplot using ggplot in R. I plotted the products on the x-axis and the amounts plotted on the y-axis. The fill is the channels.



My code looks like this:



ggplot(df, aes(x=products, y=sales, fill=channel)) + geom_bar(stat = "identity")


There are a total of 4 different saleschannels (website, email, street sales, direct message). The problem I face is that for different products different plot colors are used, because not all products have used each saleschannel for sales. Important to know is that I want to plot each product in a separate plot. For example, product1 has used all four saleeschannels and will thus use four different colors in the stacked barplot. However, product2 only used three saleschannels (website, email and street sales), and will thus use three different colors in the stacked barplot. Because of this, R uses different colors for the products for plots with products that use four channels.



Is there a way that I can always use the same colors no matter how many saleschannels a product uses?



edit: I added the code as was requested, see below:



products <- c("product1","product1","product1","product1","product1","product1","product1",
"product2","product2","product2","product2","product2","product2",
"product3","product3","product3","product3","product3","product3","product3",
"product4","product4","product4","product4","product4","product4","product4","product4")

sales <- c(5,12,14,21,8,9,11,2,5,6,8,19,21,13,14,5,22,19,17,13,12,10,8,6,9,11,15,16)
saleschannel <- c("website","website","website","email","street sales","direct message","direct message",
"website","website","email","email","street sales","street sales",
"website","website","email","email","street sales","street sales","direct message",
"website","website","email","email","street sales","street sales","direct message","direct message")

df <- data.frame(products, sales, saleschannel)
df_1 <- subset(df, products=="product1")
df_2 <- subset(df, products=="product2")

plot_product1 <- ggplot(df_1, aes(x=products, y=sales, fill=saleschannel)) + geom_bar(stat = "identity")

plot_product2 <- ggplot(df_2, aes(x=products, y=sales, fill=saleschannel)) + geom_bar(stat = "identity")

library(cowplot)
plot_grid(plot_product1, plot_product2)


This gets me the following plots:



as per data and code getting this plot










share|improve this question















marked as duplicate by aosmith r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 hour ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
    – Tung
    8 hours ago










  • Hi! I added the code as you requested, see the original post. I hope you can help me.
    – Hugovanp
    8 hours ago










  • just now i am added the plot as per the code and what u want to achieve as output?
    – sai saran
    8 hours ago










  • I would like the colors that are used for the channels to be always the same. As you can see now in the attached image, the color for "street sales" is different in plot 1 and plot 2, I would like R to always use the same color for a channel, no matter how many channels a product uses.
    – Hugovanp
    8 hours ago















up vote
2
down vote

favorite













This question already has an answer here:




  • ggplot2: How to use same colors in different plots for same factor

    3 answers




For different products I am trying to plot the sales per saleschannel in a stacked barplot using ggplot in R. I plotted the products on the x-axis and the amounts plotted on the y-axis. The fill is the channels.



My code looks like this:



ggplot(df, aes(x=products, y=sales, fill=channel)) + geom_bar(stat = "identity")


There are a total of 4 different saleschannels (website, email, street sales, direct message). The problem I face is that for different products different plot colors are used, because not all products have used each saleschannel for sales. Important to know is that I want to plot each product in a separate plot. For example, product1 has used all four saleeschannels and will thus use four different colors in the stacked barplot. However, product2 only used three saleschannels (website, email and street sales), and will thus use three different colors in the stacked barplot. Because of this, R uses different colors for the products for plots with products that use four channels.



Is there a way that I can always use the same colors no matter how many saleschannels a product uses?



edit: I added the code as was requested, see below:



products <- c("product1","product1","product1","product1","product1","product1","product1",
"product2","product2","product2","product2","product2","product2",
"product3","product3","product3","product3","product3","product3","product3",
"product4","product4","product4","product4","product4","product4","product4","product4")

sales <- c(5,12,14,21,8,9,11,2,5,6,8,19,21,13,14,5,22,19,17,13,12,10,8,6,9,11,15,16)
saleschannel <- c("website","website","website","email","street sales","direct message","direct message",
"website","website","email","email","street sales","street sales",
"website","website","email","email","street sales","street sales","direct message",
"website","website","email","email","street sales","street sales","direct message","direct message")

df <- data.frame(products, sales, saleschannel)
df_1 <- subset(df, products=="product1")
df_2 <- subset(df, products=="product2")

plot_product1 <- ggplot(df_1, aes(x=products, y=sales, fill=saleschannel)) + geom_bar(stat = "identity")

plot_product2 <- ggplot(df_2, aes(x=products, y=sales, fill=saleschannel)) + geom_bar(stat = "identity")

library(cowplot)
plot_grid(plot_product1, plot_product2)


This gets me the following plots:



as per data and code getting this plot










share|improve this question















marked as duplicate by aosmith r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 hour ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
    – Tung
    8 hours ago










  • Hi! I added the code as you requested, see the original post. I hope you can help me.
    – Hugovanp
    8 hours ago










  • just now i am added the plot as per the code and what u want to achieve as output?
    – sai saran
    8 hours ago










  • I would like the colors that are used for the channels to be always the same. As you can see now in the attached image, the color for "street sales" is different in plot 1 and plot 2, I would like R to always use the same color for a channel, no matter how many channels a product uses.
    – Hugovanp
    8 hours ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:




  • ggplot2: How to use same colors in different plots for same factor

    3 answers




For different products I am trying to plot the sales per saleschannel in a stacked barplot using ggplot in R. I plotted the products on the x-axis and the amounts plotted on the y-axis. The fill is the channels.



My code looks like this:



ggplot(df, aes(x=products, y=sales, fill=channel)) + geom_bar(stat = "identity")


There are a total of 4 different saleschannels (website, email, street sales, direct message). The problem I face is that for different products different plot colors are used, because not all products have used each saleschannel for sales. Important to know is that I want to plot each product in a separate plot. For example, product1 has used all four saleeschannels and will thus use four different colors in the stacked barplot. However, product2 only used three saleschannels (website, email and street sales), and will thus use three different colors in the stacked barplot. Because of this, R uses different colors for the products for plots with products that use four channels.



Is there a way that I can always use the same colors no matter how many saleschannels a product uses?



edit: I added the code as was requested, see below:



products <- c("product1","product1","product1","product1","product1","product1","product1",
"product2","product2","product2","product2","product2","product2",
"product3","product3","product3","product3","product3","product3","product3",
"product4","product4","product4","product4","product4","product4","product4","product4")

sales <- c(5,12,14,21,8,9,11,2,5,6,8,19,21,13,14,5,22,19,17,13,12,10,8,6,9,11,15,16)
saleschannel <- c("website","website","website","email","street sales","direct message","direct message",
"website","website","email","email","street sales","street sales",
"website","website","email","email","street sales","street sales","direct message",
"website","website","email","email","street sales","street sales","direct message","direct message")

df <- data.frame(products, sales, saleschannel)
df_1 <- subset(df, products=="product1")
df_2 <- subset(df, products=="product2")

plot_product1 <- ggplot(df_1, aes(x=products, y=sales, fill=saleschannel)) + geom_bar(stat = "identity")

plot_product2 <- ggplot(df_2, aes(x=products, y=sales, fill=saleschannel)) + geom_bar(stat = "identity")

library(cowplot)
plot_grid(plot_product1, plot_product2)


This gets me the following plots:



as per data and code getting this plot










share|improve this question
















This question already has an answer here:




  • ggplot2: How to use same colors in different plots for same factor

    3 answers




For different products I am trying to plot the sales per saleschannel in a stacked barplot using ggplot in R. I plotted the products on the x-axis and the amounts plotted on the y-axis. The fill is the channels.



My code looks like this:



ggplot(df, aes(x=products, y=sales, fill=channel)) + geom_bar(stat = "identity")


There are a total of 4 different saleschannels (website, email, street sales, direct message). The problem I face is that for different products different plot colors are used, because not all products have used each saleschannel for sales. Important to know is that I want to plot each product in a separate plot. For example, product1 has used all four saleeschannels and will thus use four different colors in the stacked barplot. However, product2 only used three saleschannels (website, email and street sales), and will thus use three different colors in the stacked barplot. Because of this, R uses different colors for the products for plots with products that use four channels.



Is there a way that I can always use the same colors no matter how many saleschannels a product uses?



edit: I added the code as was requested, see below:



products <- c("product1","product1","product1","product1","product1","product1","product1",
"product2","product2","product2","product2","product2","product2",
"product3","product3","product3","product3","product3","product3","product3",
"product4","product4","product4","product4","product4","product4","product4","product4")

sales <- c(5,12,14,21,8,9,11,2,5,6,8,19,21,13,14,5,22,19,17,13,12,10,8,6,9,11,15,16)
saleschannel <- c("website","website","website","email","street sales","direct message","direct message",
"website","website","email","email","street sales","street sales",
"website","website","email","email","street sales","street sales","direct message",
"website","website","email","email","street sales","street sales","direct message","direct message")

df <- data.frame(products, sales, saleschannel)
df_1 <- subset(df, products=="product1")
df_2 <- subset(df, products=="product2")

plot_product1 <- ggplot(df_1, aes(x=products, y=sales, fill=saleschannel)) + geom_bar(stat = "identity")

plot_product2 <- ggplot(df_2, aes(x=products, y=sales, fill=saleschannel)) + geom_bar(stat = "identity")

library(cowplot)
plot_grid(plot_product1, plot_product2)


This gets me the following plots:



as per data and code getting this plot





This question already has an answer here:




  • ggplot2: How to use same colors in different plots for same factor

    3 answers








r ggplot2 bar-chart fill geom-bar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 6 hours ago









MLavoie

6,82672242




6,82672242










asked 8 hours ago









Hugovanp

133




133




marked as duplicate by aosmith r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 hour ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by aosmith r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
1 hour ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
    – Tung
    8 hours ago










  • Hi! I added the code as you requested, see the original post. I hope you can help me.
    – Hugovanp
    8 hours ago










  • just now i am added the plot as per the code and what u want to achieve as output?
    – sai saran
    8 hours ago










  • I would like the colors that are used for the channels to be always the same. As you can see now in the attached image, the color for "street sales" is different in plot 1 and plot 2, I would like R to always use the same color for a channel, no matter how many channels a product uses.
    – Hugovanp
    8 hours ago


















  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
    – Tung
    8 hours ago










  • Hi! I added the code as you requested, see the original post. I hope you can help me.
    – Hugovanp
    8 hours ago










  • just now i am added the plot as per the code and what u want to achieve as output?
    – sai saran
    8 hours ago










  • I would like the colors that are used for the channels to be always the same. As you can see now in the attached image, the color for "street sales" is different in plot 1 and plot 2, I would like R to always use the same color for a channel, no matter how many channels a product uses.
    – Hugovanp
    8 hours ago
















Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
8 hours ago




Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
8 hours ago












Hi! I added the code as you requested, see the original post. I hope you can help me.
– Hugovanp
8 hours ago




Hi! I added the code as you requested, see the original post. I hope you can help me.
– Hugovanp
8 hours ago












just now i am added the plot as per the code and what u want to achieve as output?
– sai saran
8 hours ago




just now i am added the plot as per the code and what u want to achieve as output?
– sai saran
8 hours ago












I would like the colors that are used for the channels to be always the same. As you can see now in the attached image, the color for "street sales" is different in plot 1 and plot 2, I would like R to always use the same color for a channel, no matter how many channels a product uses.
– Hugovanp
8 hours ago




I would like the colors that are used for the channels to be always the same. As you can see now in the attached image, the color for "street sales" is different in plot 1 and plot 2, I would like R to always use the same color for a channel, no matter how many channels a product uses.
– Hugovanp
8 hours ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You can use scale_fill_manual(values = ...), together with preset colors for each saleschannel.



First, you need to get the explicit names of the colors. You can use any other colors you like, for example the packages RColorBrewer has some options. Here, I use the ggplot2 default, like shown in this SO answer:



# n: number of colors to generate
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}


Next, define a color for each sales channel by creating a named string vector, where the names are the saleschannel levels and the values are the colors:



s = unique(saleschannel)
cols = setNames(gg_color_hue(length(s)), s)
cols
## website email street sales direct message
## "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"


Add explicit color scales to your plots. In your case, the aestetic of choice is fill, therefore you need scale_fill_manual. You can also add + scale_fill_manual(...) to the end of your definitions of plot_productX:



plot_product1 <- plot_product1 + scale_fill_manual(values = cols)
plot_product2 <- plot_product2 + scale_fill_manual(values = cols)


Then use plot_grid (an alternative is gridExtra::grid.arrange) as in your question



plots with same color scale






share|improve this answer





















  • This works! Thank you very much!
    – Hugovanp
    7 hours ago










  • @Hugovanp Great :) However, this answer assumes that you don't want a plot like in sai saran's answer. If you do, their solution is simpler. Also, there is faceting, which also synchronizes the axis scales
    – akraf
    7 hours ago




















up vote
1
down vote













Can u try this code :



library(dplyr)
df1<-df%>%filter(products %in% c( "product1" ,"product2"))

ggplot(df1,aes(x=products,y=sales,fill=saleschannel))+geom_bar(stat="identity")


updated one






share|improve this answer

















  • 1




    This works if you want to plot it in a single plot, however, I want it in separate plots. I already have the solution (see answer akraf above). Thanks for your suggestion!
    – Hugovanp
    7 hours ago




















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You can use scale_fill_manual(values = ...), together with preset colors for each saleschannel.



First, you need to get the explicit names of the colors. You can use any other colors you like, for example the packages RColorBrewer has some options. Here, I use the ggplot2 default, like shown in this SO answer:



# n: number of colors to generate
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}


Next, define a color for each sales channel by creating a named string vector, where the names are the saleschannel levels and the values are the colors:



s = unique(saleschannel)
cols = setNames(gg_color_hue(length(s)), s)
cols
## website email street sales direct message
## "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"


Add explicit color scales to your plots. In your case, the aestetic of choice is fill, therefore you need scale_fill_manual. You can also add + scale_fill_manual(...) to the end of your definitions of plot_productX:



plot_product1 <- plot_product1 + scale_fill_manual(values = cols)
plot_product2 <- plot_product2 + scale_fill_manual(values = cols)


Then use plot_grid (an alternative is gridExtra::grid.arrange) as in your question



plots with same color scale






share|improve this answer





















  • This works! Thank you very much!
    – Hugovanp
    7 hours ago










  • @Hugovanp Great :) However, this answer assumes that you don't want a plot like in sai saran's answer. If you do, their solution is simpler. Also, there is faceting, which also synchronizes the axis scales
    – akraf
    7 hours ago

















up vote
1
down vote



accepted










You can use scale_fill_manual(values = ...), together with preset colors for each saleschannel.



First, you need to get the explicit names of the colors. You can use any other colors you like, for example the packages RColorBrewer has some options. Here, I use the ggplot2 default, like shown in this SO answer:



# n: number of colors to generate
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}


Next, define a color for each sales channel by creating a named string vector, where the names are the saleschannel levels and the values are the colors:



s = unique(saleschannel)
cols = setNames(gg_color_hue(length(s)), s)
cols
## website email street sales direct message
## "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"


Add explicit color scales to your plots. In your case, the aestetic of choice is fill, therefore you need scale_fill_manual. You can also add + scale_fill_manual(...) to the end of your definitions of plot_productX:



plot_product1 <- plot_product1 + scale_fill_manual(values = cols)
plot_product2 <- plot_product2 + scale_fill_manual(values = cols)


Then use plot_grid (an alternative is gridExtra::grid.arrange) as in your question



plots with same color scale






share|improve this answer





















  • This works! Thank you very much!
    – Hugovanp
    7 hours ago










  • @Hugovanp Great :) However, this answer assumes that you don't want a plot like in sai saran's answer. If you do, their solution is simpler. Also, there is faceting, which also synchronizes the axis scales
    – akraf
    7 hours ago















up vote
1
down vote



accepted







up vote
1
down vote



accepted






You can use scale_fill_manual(values = ...), together with preset colors for each saleschannel.



First, you need to get the explicit names of the colors. You can use any other colors you like, for example the packages RColorBrewer has some options. Here, I use the ggplot2 default, like shown in this SO answer:



# n: number of colors to generate
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}


Next, define a color for each sales channel by creating a named string vector, where the names are the saleschannel levels and the values are the colors:



s = unique(saleschannel)
cols = setNames(gg_color_hue(length(s)), s)
cols
## website email street sales direct message
## "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"


Add explicit color scales to your plots. In your case, the aestetic of choice is fill, therefore you need scale_fill_manual. You can also add + scale_fill_manual(...) to the end of your definitions of plot_productX:



plot_product1 <- plot_product1 + scale_fill_manual(values = cols)
plot_product2 <- plot_product2 + scale_fill_manual(values = cols)


Then use plot_grid (an alternative is gridExtra::grid.arrange) as in your question



plots with same color scale






share|improve this answer












You can use scale_fill_manual(values = ...), together with preset colors for each saleschannel.



First, you need to get the explicit names of the colors. You can use any other colors you like, for example the packages RColorBrewer has some options. Here, I use the ggplot2 default, like shown in this SO answer:



# n: number of colors to generate
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}


Next, define a color for each sales channel by creating a named string vector, where the names are the saleschannel levels and the values are the colors:



s = unique(saleschannel)
cols = setNames(gg_color_hue(length(s)), s)
cols
## website email street sales direct message
## "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"


Add explicit color scales to your plots. In your case, the aestetic of choice is fill, therefore you need scale_fill_manual. You can also add + scale_fill_manual(...) to the end of your definitions of plot_productX:



plot_product1 <- plot_product1 + scale_fill_manual(values = cols)
plot_product2 <- plot_product2 + scale_fill_manual(values = cols)


Then use plot_grid (an alternative is gridExtra::grid.arrange) as in your question



plots with same color scale







share|improve this answer












share|improve this answer



share|improve this answer










answered 7 hours ago









akraf

1,345622




1,345622












  • This works! Thank you very much!
    – Hugovanp
    7 hours ago










  • @Hugovanp Great :) However, this answer assumes that you don't want a plot like in sai saran's answer. If you do, their solution is simpler. Also, there is faceting, which also synchronizes the axis scales
    – akraf
    7 hours ago




















  • This works! Thank you very much!
    – Hugovanp
    7 hours ago










  • @Hugovanp Great :) However, this answer assumes that you don't want a plot like in sai saran's answer. If you do, their solution is simpler. Also, there is faceting, which also synchronizes the axis scales
    – akraf
    7 hours ago


















This works! Thank you very much!
– Hugovanp
7 hours ago




This works! Thank you very much!
– Hugovanp
7 hours ago












@Hugovanp Great :) However, this answer assumes that you don't want a plot like in sai saran's answer. If you do, their solution is simpler. Also, there is faceting, which also synchronizes the axis scales
– akraf
7 hours ago






@Hugovanp Great :) However, this answer assumes that you don't want a plot like in sai saran's answer. If you do, their solution is simpler. Also, there is faceting, which also synchronizes the axis scales
– akraf
7 hours ago














up vote
1
down vote













Can u try this code :



library(dplyr)
df1<-df%>%filter(products %in% c( "product1" ,"product2"))

ggplot(df1,aes(x=products,y=sales,fill=saleschannel))+geom_bar(stat="identity")


updated one






share|improve this answer

















  • 1




    This works if you want to plot it in a single plot, however, I want it in separate plots. I already have the solution (see answer akraf above). Thanks for your suggestion!
    – Hugovanp
    7 hours ago

















up vote
1
down vote













Can u try this code :



library(dplyr)
df1<-df%>%filter(products %in% c( "product1" ,"product2"))

ggplot(df1,aes(x=products,y=sales,fill=saleschannel))+geom_bar(stat="identity")


updated one






share|improve this answer

















  • 1




    This works if you want to plot it in a single plot, however, I want it in separate plots. I already have the solution (see answer akraf above). Thanks for your suggestion!
    – Hugovanp
    7 hours ago















up vote
1
down vote










up vote
1
down vote









Can u try this code :



library(dplyr)
df1<-df%>%filter(products %in% c( "product1" ,"product2"))

ggplot(df1,aes(x=products,y=sales,fill=saleschannel))+geom_bar(stat="identity")


updated one






share|improve this answer












Can u try this code :



library(dplyr)
df1<-df%>%filter(products %in% c( "product1" ,"product2"))

ggplot(df1,aes(x=products,y=sales,fill=saleschannel))+geom_bar(stat="identity")


updated one







share|improve this answer












share|improve this answer



share|improve this answer










answered 7 hours ago









sai saran

41817




41817








  • 1




    This works if you want to plot it in a single plot, however, I want it in separate plots. I already have the solution (see answer akraf above). Thanks for your suggestion!
    – Hugovanp
    7 hours ago
















  • 1




    This works if you want to plot it in a single plot, however, I want it in separate plots. I already have the solution (see answer akraf above). Thanks for your suggestion!
    – Hugovanp
    7 hours ago










1




1




This works if you want to plot it in a single plot, however, I want it in separate plots. I already have the solution (see answer akraf above). Thanks for your suggestion!
– Hugovanp
7 hours ago






This works if you want to plot it in a single plot, however, I want it in separate plots. I already have the solution (see answer akraf above). Thanks for your suggestion!
– Hugovanp
7 hours ago





Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)