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:
r ggplot2 bar-chart fill geom-bar
marked as duplicate by aosmith
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();
}
);
});
});
Nov 8 at 16:01
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.
add a comment |
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:
r ggplot2 bar-chart fill geom-bar
marked as duplicate by aosmith
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();
}
);
});
});
Nov 8 at 16:01
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 usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
Nov 8 at 9:04
Hi! I added the code as you requested, see the original post. I hope you can help me.
– Hugovanp
Nov 8 at 9:37
just now i am added the plot as per the code and what u want to achieve as output?
– sai saran
Nov 8 at 9:40
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
Nov 8 at 9:44
add a comment |
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:
r ggplot2 bar-chart fill geom-bar
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:
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
r ggplot2 bar-chart fill geom-bar
edited Nov 8 at 11:30
MLavoie
6,82672242
6,82672242
asked Nov 8 at 9:02
Hugovanp
133
133
marked as duplicate by aosmith
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();
}
);
});
});
Nov 8 at 16:01
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
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();
}
);
});
});
Nov 8 at 16:01
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 usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
Nov 8 at 9:04
Hi! I added the code as you requested, see the original post. I hope you can help me.
– Hugovanp
Nov 8 at 9:37
just now i am added the plot as per the code and what u want to achieve as output?
– sai saran
Nov 8 at 9:40
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
Nov 8 at 9:44
add a comment |
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 usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
Nov 8 at 9:04
Hi! I added the code as you requested, see the original post. I hope you can help me.
– Hugovanp
Nov 8 at 9:37
just now i am added the plot as per the code and what u want to achieve as output?
– sai saran
Nov 8 at 9:40
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
Nov 8 at 9:44
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
Nov 8 at 9:04
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
Nov 8 at 9:04
Hi! I added the code as you requested, see the original post. I hope you can help me.
– Hugovanp
Nov 8 at 9:37
Hi! I added the code as you requested, see the original post. I hope you can help me.
– Hugovanp
Nov 8 at 9:37
just now i am added the plot as per the code and what u want to achieve as output?
– sai saran
Nov 8 at 9:40
just now i am added the plot as per the code and what u want to achieve as output?
– sai saran
Nov 8 at 9:40
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
Nov 8 at 9:44
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
Nov 8 at 9:44
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
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
This works! Thank you very much!
– Hugovanp
Nov 8 at 10:17
@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
Nov 8 at 10:19
add a comment |
up vote
2
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")
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
Nov 8 at 10:18
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
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
This works! Thank you very much!
– Hugovanp
Nov 8 at 10:17
@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
Nov 8 at 10:19
add a comment |
up vote
2
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
This works! Thank you very much!
– Hugovanp
Nov 8 at 10:17
@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
Nov 8 at 10:19
add a comment |
up vote
2
down vote
accepted
up vote
2
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
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
answered Nov 8 at 10:02
akraf
1,380622
1,380622
This works! Thank you very much!
– Hugovanp
Nov 8 at 10:17
@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
Nov 8 at 10:19
add a comment |
This works! Thank you very much!
– Hugovanp
Nov 8 at 10:17
@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
Nov 8 at 10:19
This works! Thank you very much!
– Hugovanp
Nov 8 at 10:17
This works! Thank you very much!
– Hugovanp
Nov 8 at 10:17
@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
Nov 8 at 10:19
@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
Nov 8 at 10:19
add a comment |
up vote
2
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")
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
Nov 8 at 10:18
add a comment |
up vote
2
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")
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
Nov 8 at 10:18
add a comment |
up vote
2
down vote
up vote
2
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")
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")
answered Nov 8 at 10:05
sai saran
66520
66520
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
Nov 8 at 10:18
add a comment |
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
Nov 8 at 10:18
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
Nov 8 at 10:18
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
Nov 8 at 10:18
add a comment |
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 thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?– Tung
Nov 8 at 9:04
Hi! I added the code as you requested, see the original post. I hope you can help me.
– Hugovanp
Nov 8 at 9:37
just now i am added the plot as per the code and what u want to achieve as output?
– sai saran
Nov 8 at 9:40
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
Nov 8 at 9:44