Error: Change color in a stacked bar plor ggplot2
I want to change the color in a stacked barplot using ggplot2
. I searched for a while on the internet and I have found some suggestions, but they didn't work.
I tried:
scale_fill_grey(start=0.5, end=0)
and
scale_color_gradient(11="dark", 22="grey")
and
scale_fill_manual(values=c('#999999','#E69F00'))
They are all not working. Can anyone help me? Thank you very much in advance.
dput(IC)
structure(list(station = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L,
5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), morning = c(11L,
22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L,
11L, 22L, 11L, 22L, 11L, 22L), number = c(52L, 48L, 38L, 48L,
68L, 32L, 144L, 63L, 125L, 40L, 37L, 9L, 3L, 0L, 18L, 6L, 13L,
35L, 39L, 60L)), class = "data.frame", row.names = c(NA, -20L
))
r ggplot2
add a comment |
I want to change the color in a stacked barplot using ggplot2
. I searched for a while on the internet and I have found some suggestions, but they didn't work.
I tried:
scale_fill_grey(start=0.5, end=0)
and
scale_color_gradient(11="dark", 22="grey")
and
scale_fill_manual(values=c('#999999','#E69F00'))
They are all not working. Can anyone help me? Thank you very much in advance.
dput(IC)
structure(list(station = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L,
5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), morning = c(11L,
22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L,
11L, 22L, 11L, 22L, 11L, 22L), number = c(52L, 48L, 38L, 48L,
68L, 32L, 144L, 63L, 125L, 40L, 37L, 9L, 3L, 0L, 18L, 6L, 13L,
35L, 39L, 60L)), class = "data.frame", row.names = c(NA, -20L
))
r ggplot2
This is my script: options(stringsAsFactors = FALSE) input <- "C:\Users\IC.csv" IC <- read.csv(input, sep=";") dput(IC) library(lubridate) library(ggplot2) plot<- ggplot(IC,aes(x=station, y=number, fill=morning)) + geom_bar(stat="identity") +theme_bw()+ scale_fill_grey(start=0.5, end=0) print(plot)
– Sea24
Jan 19 at 13:14
Check to see if this is what you need.
– NelsonGon
Jan 19 at 13:21
add a comment |
I want to change the color in a stacked barplot using ggplot2
. I searched for a while on the internet and I have found some suggestions, but they didn't work.
I tried:
scale_fill_grey(start=0.5, end=0)
and
scale_color_gradient(11="dark", 22="grey")
and
scale_fill_manual(values=c('#999999','#E69F00'))
They are all not working. Can anyone help me? Thank you very much in advance.
dput(IC)
structure(list(station = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L,
5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), morning = c(11L,
22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L,
11L, 22L, 11L, 22L, 11L, 22L), number = c(52L, 48L, 38L, 48L,
68L, 32L, 144L, 63L, 125L, 40L, 37L, 9L, 3L, 0L, 18L, 6L, 13L,
35L, 39L, 60L)), class = "data.frame", row.names = c(NA, -20L
))
r ggplot2
I want to change the color in a stacked barplot using ggplot2
. I searched for a while on the internet and I have found some suggestions, but they didn't work.
I tried:
scale_fill_grey(start=0.5, end=0)
and
scale_color_gradient(11="dark", 22="grey")
and
scale_fill_manual(values=c('#999999','#E69F00'))
They are all not working. Can anyone help me? Thank you very much in advance.
dput(IC)
structure(list(station = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L,
5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), morning = c(11L,
22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L,
11L, 22L, 11L, 22L, 11L, 22L), number = c(52L, 48L, 38L, 48L,
68L, 32L, 144L, 63L, 125L, 40L, 37L, 9L, 3L, 0L, 18L, 6L, 13L,
35L, 39L, 60L)), class = "data.frame", row.names = c(NA, -20L
))
r ggplot2
r ggplot2
edited Jan 19 at 15:04
Claudiu Papasteri
170111
170111
asked Jan 19 at 13:12
Sea24Sea24
85
85
This is my script: options(stringsAsFactors = FALSE) input <- "C:\Users\IC.csv" IC <- read.csv(input, sep=";") dput(IC) library(lubridate) library(ggplot2) plot<- ggplot(IC,aes(x=station, y=number, fill=morning)) + geom_bar(stat="identity") +theme_bw()+ scale_fill_grey(start=0.5, end=0) print(plot)
– Sea24
Jan 19 at 13:14
Check to see if this is what you need.
– NelsonGon
Jan 19 at 13:21
add a comment |
This is my script: options(stringsAsFactors = FALSE) input <- "C:\Users\IC.csv" IC <- read.csv(input, sep=";") dput(IC) library(lubridate) library(ggplot2) plot<- ggplot(IC,aes(x=station, y=number, fill=morning)) + geom_bar(stat="identity") +theme_bw()+ scale_fill_grey(start=0.5, end=0) print(plot)
– Sea24
Jan 19 at 13:14
Check to see if this is what you need.
– NelsonGon
Jan 19 at 13:21
This is my script: options(stringsAsFactors = FALSE) input <- "C:\Users\IC.csv" IC <- read.csv(input, sep=";") dput(IC) library(lubridate) library(ggplot2) plot<- ggplot(IC,aes(x=station, y=number, fill=morning)) + geom_bar(stat="identity") +theme_bw()+ scale_fill_grey(start=0.5, end=0) print(plot)
– Sea24
Jan 19 at 13:14
This is my script: options(stringsAsFactors = FALSE) input <- "C:\Users\IC.csv" IC <- read.csv(input, sep=";") dput(IC) library(lubridate) library(ggplot2) plot<- ggplot(IC,aes(x=station, y=number, fill=morning)) + geom_bar(stat="identity") +theme_bw()+ scale_fill_grey(start=0.5, end=0) print(plot)
– Sea24
Jan 19 at 13:14
Check to see if this is what you need.
– NelsonGon
Jan 19 at 13:21
Check to see if this is what you need.
– NelsonGon
Jan 19 at 13:21
add a comment |
1 Answer
1
active
oldest
votes
To change to the y-axis to percent format, use the scales
package. I've given an answer on the same here: Stacked barplot with percentage in R ggplot2 for categorical variables from scratch
library(tidyverse)
library(scales)
IC %>%
mutate(morning=as.factor(morning),number=as.numeric(number)) %>%
ggplot(aes(station,number,fill=morning))+geom_bar(stat="identity",position = "fill")+
scale_y_continuous(labels=scales::percent_format(),breaks=scales::pretty_breaks(n=10))+
scale_fill_manual(values=c('#999999','#E69F00'))
1
I am sorry. I had a typing error. Now it is working with your code: ICI_morning %>% mutate(morning=as.factor(morning)) %>% ggplot(aes(x=station,y=number,fill=morning))+geom_bar(stat="identity",position = "fill")+ scale_fill_manual(values=c('#999999','#E69F00'))
– Sea24
Jan 19 at 13:40
1
Thank you very much!
– Sea24
Jan 19 at 13:45
1
I am sorry. I have one more question: I can't change the y-axis anymore. The graph in R has the y-axis label: 0.25, 0.50, 0.75, 1.00. But in my Excel sheet it is in %, e.g. 25, 50, 75, 100. How can I change this in R. When I try scale_y_continous(limits=c(0,100), breaks=seq(0,100,10)), it is not working.
– Sea24
Jan 19 at 14:54
1
Thank you very much. Now, I have the % at the axis, but I can't change the scale. I would like to have the axis in intervals of ten. And this is not working: scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10),labels=scales::percent_format())
– Sea24
Jan 19 at 15:20
1
Thank you so much. It's perfect.
– Sea24
Jan 19 at 15:57
|
show 8 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267464%2ferror-change-color-in-a-stacked-bar-plor-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To change to the y-axis to percent format, use the scales
package. I've given an answer on the same here: Stacked barplot with percentage in R ggplot2 for categorical variables from scratch
library(tidyverse)
library(scales)
IC %>%
mutate(morning=as.factor(morning),number=as.numeric(number)) %>%
ggplot(aes(station,number,fill=morning))+geom_bar(stat="identity",position = "fill")+
scale_y_continuous(labels=scales::percent_format(),breaks=scales::pretty_breaks(n=10))+
scale_fill_manual(values=c('#999999','#E69F00'))
1
I am sorry. I had a typing error. Now it is working with your code: ICI_morning %>% mutate(morning=as.factor(morning)) %>% ggplot(aes(x=station,y=number,fill=morning))+geom_bar(stat="identity",position = "fill")+ scale_fill_manual(values=c('#999999','#E69F00'))
– Sea24
Jan 19 at 13:40
1
Thank you very much!
– Sea24
Jan 19 at 13:45
1
I am sorry. I have one more question: I can't change the y-axis anymore. The graph in R has the y-axis label: 0.25, 0.50, 0.75, 1.00. But in my Excel sheet it is in %, e.g. 25, 50, 75, 100. How can I change this in R. When I try scale_y_continous(limits=c(0,100), breaks=seq(0,100,10)), it is not working.
– Sea24
Jan 19 at 14:54
1
Thank you very much. Now, I have the % at the axis, but I can't change the scale. I would like to have the axis in intervals of ten. And this is not working: scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10),labels=scales::percent_format())
– Sea24
Jan 19 at 15:20
1
Thank you so much. It's perfect.
– Sea24
Jan 19 at 15:57
|
show 8 more comments
To change to the y-axis to percent format, use the scales
package. I've given an answer on the same here: Stacked barplot with percentage in R ggplot2 for categorical variables from scratch
library(tidyverse)
library(scales)
IC %>%
mutate(morning=as.factor(morning),number=as.numeric(number)) %>%
ggplot(aes(station,number,fill=morning))+geom_bar(stat="identity",position = "fill")+
scale_y_continuous(labels=scales::percent_format(),breaks=scales::pretty_breaks(n=10))+
scale_fill_manual(values=c('#999999','#E69F00'))
1
I am sorry. I had a typing error. Now it is working with your code: ICI_morning %>% mutate(morning=as.factor(morning)) %>% ggplot(aes(x=station,y=number,fill=morning))+geom_bar(stat="identity",position = "fill")+ scale_fill_manual(values=c('#999999','#E69F00'))
– Sea24
Jan 19 at 13:40
1
Thank you very much!
– Sea24
Jan 19 at 13:45
1
I am sorry. I have one more question: I can't change the y-axis anymore. The graph in R has the y-axis label: 0.25, 0.50, 0.75, 1.00. But in my Excel sheet it is in %, e.g. 25, 50, 75, 100. How can I change this in R. When I try scale_y_continous(limits=c(0,100), breaks=seq(0,100,10)), it is not working.
– Sea24
Jan 19 at 14:54
1
Thank you very much. Now, I have the % at the axis, but I can't change the scale. I would like to have the axis in intervals of ten. And this is not working: scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10),labels=scales::percent_format())
– Sea24
Jan 19 at 15:20
1
Thank you so much. It's perfect.
– Sea24
Jan 19 at 15:57
|
show 8 more comments
To change to the y-axis to percent format, use the scales
package. I've given an answer on the same here: Stacked barplot with percentage in R ggplot2 for categorical variables from scratch
library(tidyverse)
library(scales)
IC %>%
mutate(morning=as.factor(morning),number=as.numeric(number)) %>%
ggplot(aes(station,number,fill=morning))+geom_bar(stat="identity",position = "fill")+
scale_y_continuous(labels=scales::percent_format(),breaks=scales::pretty_breaks(n=10))+
scale_fill_manual(values=c('#999999','#E69F00'))
To change to the y-axis to percent format, use the scales
package. I've given an answer on the same here: Stacked barplot with percentage in R ggplot2 for categorical variables from scratch
library(tidyverse)
library(scales)
IC %>%
mutate(morning=as.factor(morning),number=as.numeric(number)) %>%
ggplot(aes(station,number,fill=morning))+geom_bar(stat="identity",position = "fill")+
scale_y_continuous(labels=scales::percent_format(),breaks=scales::pretty_breaks(n=10))+
scale_fill_manual(values=c('#999999','#E69F00'))
edited Jan 19 at 15:37
answered Jan 19 at 13:15
NelsonGonNelsonGon
2,3011626
2,3011626
1
I am sorry. I had a typing error. Now it is working with your code: ICI_morning %>% mutate(morning=as.factor(morning)) %>% ggplot(aes(x=station,y=number,fill=morning))+geom_bar(stat="identity",position = "fill")+ scale_fill_manual(values=c('#999999','#E69F00'))
– Sea24
Jan 19 at 13:40
1
Thank you very much!
– Sea24
Jan 19 at 13:45
1
I am sorry. I have one more question: I can't change the y-axis anymore. The graph in R has the y-axis label: 0.25, 0.50, 0.75, 1.00. But in my Excel sheet it is in %, e.g. 25, 50, 75, 100. How can I change this in R. When I try scale_y_continous(limits=c(0,100), breaks=seq(0,100,10)), it is not working.
– Sea24
Jan 19 at 14:54
1
Thank you very much. Now, I have the % at the axis, but I can't change the scale. I would like to have the axis in intervals of ten. And this is not working: scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10),labels=scales::percent_format())
– Sea24
Jan 19 at 15:20
1
Thank you so much. It's perfect.
– Sea24
Jan 19 at 15:57
|
show 8 more comments
1
I am sorry. I had a typing error. Now it is working with your code: ICI_morning %>% mutate(morning=as.factor(morning)) %>% ggplot(aes(x=station,y=number,fill=morning))+geom_bar(stat="identity",position = "fill")+ scale_fill_manual(values=c('#999999','#E69F00'))
– Sea24
Jan 19 at 13:40
1
Thank you very much!
– Sea24
Jan 19 at 13:45
1
I am sorry. I have one more question: I can't change the y-axis anymore. The graph in R has the y-axis label: 0.25, 0.50, 0.75, 1.00. But in my Excel sheet it is in %, e.g. 25, 50, 75, 100. How can I change this in R. When I try scale_y_continous(limits=c(0,100), breaks=seq(0,100,10)), it is not working.
– Sea24
Jan 19 at 14:54
1
Thank you very much. Now, I have the % at the axis, but I can't change the scale. I would like to have the axis in intervals of ten. And this is not working: scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10),labels=scales::percent_format())
– Sea24
Jan 19 at 15:20
1
Thank you so much. It's perfect.
– Sea24
Jan 19 at 15:57
1
1
I am sorry. I had a typing error. Now it is working with your code: ICI_morning %>% mutate(morning=as.factor(morning)) %>% ggplot(aes(x=station,y=number,fill=morning))+geom_bar(stat="identity",position = "fill")+ scale_fill_manual(values=c('#999999','#E69F00'))
– Sea24
Jan 19 at 13:40
I am sorry. I had a typing error. Now it is working with your code: ICI_morning %>% mutate(morning=as.factor(morning)) %>% ggplot(aes(x=station,y=number,fill=morning))+geom_bar(stat="identity",position = "fill")+ scale_fill_manual(values=c('#999999','#E69F00'))
– Sea24
Jan 19 at 13:40
1
1
Thank you very much!
– Sea24
Jan 19 at 13:45
Thank you very much!
– Sea24
Jan 19 at 13:45
1
1
I am sorry. I have one more question: I can't change the y-axis anymore. The graph in R has the y-axis label: 0.25, 0.50, 0.75, 1.00. But in my Excel sheet it is in %, e.g. 25, 50, 75, 100. How can I change this in R. When I try scale_y_continous(limits=c(0,100), breaks=seq(0,100,10)), it is not working.
– Sea24
Jan 19 at 14:54
I am sorry. I have one more question: I can't change the y-axis anymore. The graph in R has the y-axis label: 0.25, 0.50, 0.75, 1.00. But in my Excel sheet it is in %, e.g. 25, 50, 75, 100. How can I change this in R. When I try scale_y_continous(limits=c(0,100), breaks=seq(0,100,10)), it is not working.
– Sea24
Jan 19 at 14:54
1
1
Thank you very much. Now, I have the % at the axis, but I can't change the scale. I would like to have the axis in intervals of ten. And this is not working: scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10),labels=scales::percent_format())
– Sea24
Jan 19 at 15:20
Thank you very much. Now, I have the % at the axis, but I can't change the scale. I would like to have the axis in intervals of ten. And this is not working: scale_y_continuous(limits=c(0,100), breaks=seq(0,100,10),labels=scales::percent_format())
– Sea24
Jan 19 at 15:20
1
1
Thank you so much. It's perfect.
– Sea24
Jan 19 at 15:57
Thank you so much. It's perfect.
– Sea24
Jan 19 at 15:57
|
show 8 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267464%2ferror-change-color-in-a-stacked-bar-plor-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This is my script: options(stringsAsFactors = FALSE) input <- "C:\Users\IC.csv" IC <- read.csv(input, sep=";") dput(IC) library(lubridate) library(ggplot2) plot<- ggplot(IC,aes(x=station, y=number, fill=morning)) + geom_bar(stat="identity") +theme_bw()+ scale_fill_grey(start=0.5, end=0) print(plot)
– Sea24
Jan 19 at 13:14
Check to see if this is what you need.
– NelsonGon
Jan 19 at 13:21