ggplot: Desaturate/Make gray bars based on column value

Multi tool use
Multi tool use












1















I would like to desaturate (or make gray) certain bars in a bar chart. I figured out how to make certain bars more transparent than others based on a column value using alpha=column_name in geom_bar. But I would like to make them gray not transparent. Is there a way to do it natively in ggplot?



Below is the code to make certain columns transparent based on column S. Is there a way to desaturate/make gray specific bars based on column value?



Edited code to include data.



data_t<-as.data.frame(Titanic)

join_table<- data_t %>% group_by(X=Class) %>% summarise(Y=sum(Freq)) %>%
mutate(S=ifelse(Y-mean(Y)>0,"a","b")) %>% select(X, S); join_table

data_t %>% group_by(X=Class, groups=Sex) %>% summarise(Y=sum(Freq)) %>%
inner_join(join_table, by=c("X")) %>%
ggplot(aes(x=X, y=Y)) + theme_minimal() +
geom_bar(aes(fill=groups, alpha=S), stat = "identity", position = "dodge")


Thanks in advance!










share|improve this question




















  • 2





    You'll have more chances to have an answer if you dput(your_final_data): in one hand, generally it's not a good idea to download files given by strangers (we are all strangers here), in the other hand your issues are on the plotting part, so the data manipulation is maybe useless.

    – s_t
    Dec 21 '18 at 8:15











  • Thank you. That was a valuable comment. I edited code to include data

    – user13874
    Dec 21 '18 at 8:54











  • So, in your example the first four bars should remain the same (red, turquoise) and then the other four should be grey?

    – Julius Vainora
    Dec 21 '18 at 14:40











  • Yep. That is correct

    – user13874
    Dec 21 '18 at 15:42
















1















I would like to desaturate (or make gray) certain bars in a bar chart. I figured out how to make certain bars more transparent than others based on a column value using alpha=column_name in geom_bar. But I would like to make them gray not transparent. Is there a way to do it natively in ggplot?



Below is the code to make certain columns transparent based on column S. Is there a way to desaturate/make gray specific bars based on column value?



Edited code to include data.



data_t<-as.data.frame(Titanic)

join_table<- data_t %>% group_by(X=Class) %>% summarise(Y=sum(Freq)) %>%
mutate(S=ifelse(Y-mean(Y)>0,"a","b")) %>% select(X, S); join_table

data_t %>% group_by(X=Class, groups=Sex) %>% summarise(Y=sum(Freq)) %>%
inner_join(join_table, by=c("X")) %>%
ggplot(aes(x=X, y=Y)) + theme_minimal() +
geom_bar(aes(fill=groups, alpha=S), stat = "identity", position = "dodge")


Thanks in advance!










share|improve this question




















  • 2





    You'll have more chances to have an answer if you dput(your_final_data): in one hand, generally it's not a good idea to download files given by strangers (we are all strangers here), in the other hand your issues are on the plotting part, so the data manipulation is maybe useless.

    – s_t
    Dec 21 '18 at 8:15











  • Thank you. That was a valuable comment. I edited code to include data

    – user13874
    Dec 21 '18 at 8:54











  • So, in your example the first four bars should remain the same (red, turquoise) and then the other four should be grey?

    – Julius Vainora
    Dec 21 '18 at 14:40











  • Yep. That is correct

    – user13874
    Dec 21 '18 at 15:42














1












1








1








I would like to desaturate (or make gray) certain bars in a bar chart. I figured out how to make certain bars more transparent than others based on a column value using alpha=column_name in geom_bar. But I would like to make them gray not transparent. Is there a way to do it natively in ggplot?



Below is the code to make certain columns transparent based on column S. Is there a way to desaturate/make gray specific bars based on column value?



Edited code to include data.



data_t<-as.data.frame(Titanic)

join_table<- data_t %>% group_by(X=Class) %>% summarise(Y=sum(Freq)) %>%
mutate(S=ifelse(Y-mean(Y)>0,"a","b")) %>% select(X, S); join_table

data_t %>% group_by(X=Class, groups=Sex) %>% summarise(Y=sum(Freq)) %>%
inner_join(join_table, by=c("X")) %>%
ggplot(aes(x=X, y=Y)) + theme_minimal() +
geom_bar(aes(fill=groups, alpha=S), stat = "identity", position = "dodge")


Thanks in advance!










share|improve this question
















I would like to desaturate (or make gray) certain bars in a bar chart. I figured out how to make certain bars more transparent than others based on a column value using alpha=column_name in geom_bar. But I would like to make them gray not transparent. Is there a way to do it natively in ggplot?



Below is the code to make certain columns transparent based on column S. Is there a way to desaturate/make gray specific bars based on column value?



Edited code to include data.



data_t<-as.data.frame(Titanic)

join_table<- data_t %>% group_by(X=Class) %>% summarise(Y=sum(Freq)) %>%
mutate(S=ifelse(Y-mean(Y)>0,"a","b")) %>% select(X, S); join_table

data_t %>% group_by(X=Class, groups=Sex) %>% summarise(Y=sum(Freq)) %>%
inner_join(join_table, by=c("X")) %>%
ggplot(aes(x=X, y=Y)) + theme_minimal() +
geom_bar(aes(fill=groups, alpha=S), stat = "identity", position = "dodge")


Thanks in advance!







r ggplot2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 21 '18 at 13:11









s_t

3,25821031




3,25821031










asked Dec 21 '18 at 6:30









user13874user13874

204




204








  • 2





    You'll have more chances to have an answer if you dput(your_final_data): in one hand, generally it's not a good idea to download files given by strangers (we are all strangers here), in the other hand your issues are on the plotting part, so the data manipulation is maybe useless.

    – s_t
    Dec 21 '18 at 8:15











  • Thank you. That was a valuable comment. I edited code to include data

    – user13874
    Dec 21 '18 at 8:54











  • So, in your example the first four bars should remain the same (red, turquoise) and then the other four should be grey?

    – Julius Vainora
    Dec 21 '18 at 14:40











  • Yep. That is correct

    – user13874
    Dec 21 '18 at 15:42














  • 2





    You'll have more chances to have an answer if you dput(your_final_data): in one hand, generally it's not a good idea to download files given by strangers (we are all strangers here), in the other hand your issues are on the plotting part, so the data manipulation is maybe useless.

    – s_t
    Dec 21 '18 at 8:15











  • Thank you. That was a valuable comment. I edited code to include data

    – user13874
    Dec 21 '18 at 8:54











  • So, in your example the first four bars should remain the same (red, turquoise) and then the other four should be grey?

    – Julius Vainora
    Dec 21 '18 at 14:40











  • Yep. That is correct

    – user13874
    Dec 21 '18 at 15:42








2




2





You'll have more chances to have an answer if you dput(your_final_data): in one hand, generally it's not a good idea to download files given by strangers (we are all strangers here), in the other hand your issues are on the plotting part, so the data manipulation is maybe useless.

– s_t
Dec 21 '18 at 8:15





You'll have more chances to have an answer if you dput(your_final_data): in one hand, generally it's not a good idea to download files given by strangers (we are all strangers here), in the other hand your issues are on the plotting part, so the data manipulation is maybe useless.

– s_t
Dec 21 '18 at 8:15













Thank you. That was a valuable comment. I edited code to include data

– user13874
Dec 21 '18 at 8:54





Thank you. That was a valuable comment. I edited code to include data

– user13874
Dec 21 '18 at 8:54













So, in your example the first four bars should remain the same (red, turquoise) and then the other four should be grey?

– Julius Vainora
Dec 21 '18 at 14:40





So, in your example the first four bars should remain the same (red, turquoise) and then the other four should be grey?

– Julius Vainora
Dec 21 '18 at 14:40













Yep. That is correct

– user13874
Dec 21 '18 at 15:42





Yep. That is correct

– user13874
Dec 21 '18 at 15:42












1 Answer
1






active

oldest

votes


















1














You can subset the data plotted each time and adjust the fill colour accordingly. The following should work:



library(tidyverse)
data_t <- as.data.frame(Titanic)

join_table <- data_t %>%
group_by(X = Class) %>%
summarise(Y = sum(Freq)) %>%
mutate(S = ifelse(Y - mean(Y) > 0, "a", "b")) %>%
select(X, S)

data_t %>%
group_by(X = Class, groups = Sex) %>%
summarise(Y = sum(Freq)) %>%
inner_join(join_table, by = c("X")) %>%
ggplot(aes(x = X, y = Y)) +
theme_minimal() +
geom_bar(data = . %>% filter( S == "b"),
aes(fill = groups), stat = "identity", position = "dodge") +
geom_bar(data = . %>% filter( S == "a"),
aes(group = groups),
fill = "grey80", stat = "identity", position = "dodge")


You can adjust fill = "grey80" to get the exact colour you'd like.






share|improve this answer
























  • This is exactly what I was looking for! Thank you!

    – user13874
    Dec 31 '18 at 22:45











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53880175%2fggplot-desaturate-make-gray-bars-based-on-column-value%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









1














You can subset the data plotted each time and adjust the fill colour accordingly. The following should work:



library(tidyverse)
data_t <- as.data.frame(Titanic)

join_table <- data_t %>%
group_by(X = Class) %>%
summarise(Y = sum(Freq)) %>%
mutate(S = ifelse(Y - mean(Y) > 0, "a", "b")) %>%
select(X, S)

data_t %>%
group_by(X = Class, groups = Sex) %>%
summarise(Y = sum(Freq)) %>%
inner_join(join_table, by = c("X")) %>%
ggplot(aes(x = X, y = Y)) +
theme_minimal() +
geom_bar(data = . %>% filter( S == "b"),
aes(fill = groups), stat = "identity", position = "dodge") +
geom_bar(data = . %>% filter( S == "a"),
aes(group = groups),
fill = "grey80", stat = "identity", position = "dodge")


You can adjust fill = "grey80" to get the exact colour you'd like.






share|improve this answer
























  • This is exactly what I was looking for! Thank you!

    – user13874
    Dec 31 '18 at 22:45
















1














You can subset the data plotted each time and adjust the fill colour accordingly. The following should work:



library(tidyverse)
data_t <- as.data.frame(Titanic)

join_table <- data_t %>%
group_by(X = Class) %>%
summarise(Y = sum(Freq)) %>%
mutate(S = ifelse(Y - mean(Y) > 0, "a", "b")) %>%
select(X, S)

data_t %>%
group_by(X = Class, groups = Sex) %>%
summarise(Y = sum(Freq)) %>%
inner_join(join_table, by = c("X")) %>%
ggplot(aes(x = X, y = Y)) +
theme_minimal() +
geom_bar(data = . %>% filter( S == "b"),
aes(fill = groups), stat = "identity", position = "dodge") +
geom_bar(data = . %>% filter( S == "a"),
aes(group = groups),
fill = "grey80", stat = "identity", position = "dodge")


You can adjust fill = "grey80" to get the exact colour you'd like.






share|improve this answer
























  • This is exactly what I was looking for! Thank you!

    – user13874
    Dec 31 '18 at 22:45














1












1








1







You can subset the data plotted each time and adjust the fill colour accordingly. The following should work:



library(tidyverse)
data_t <- as.data.frame(Titanic)

join_table <- data_t %>%
group_by(X = Class) %>%
summarise(Y = sum(Freq)) %>%
mutate(S = ifelse(Y - mean(Y) > 0, "a", "b")) %>%
select(X, S)

data_t %>%
group_by(X = Class, groups = Sex) %>%
summarise(Y = sum(Freq)) %>%
inner_join(join_table, by = c("X")) %>%
ggplot(aes(x = X, y = Y)) +
theme_minimal() +
geom_bar(data = . %>% filter( S == "b"),
aes(fill = groups), stat = "identity", position = "dodge") +
geom_bar(data = . %>% filter( S == "a"),
aes(group = groups),
fill = "grey80", stat = "identity", position = "dodge")


You can adjust fill = "grey80" to get the exact colour you'd like.






share|improve this answer













You can subset the data plotted each time and adjust the fill colour accordingly. The following should work:



library(tidyverse)
data_t <- as.data.frame(Titanic)

join_table <- data_t %>%
group_by(X = Class) %>%
summarise(Y = sum(Freq)) %>%
mutate(S = ifelse(Y - mean(Y) > 0, "a", "b")) %>%
select(X, S)

data_t %>%
group_by(X = Class, groups = Sex) %>%
summarise(Y = sum(Freq)) %>%
inner_join(join_table, by = c("X")) %>%
ggplot(aes(x = X, y = Y)) +
theme_minimal() +
geom_bar(data = . %>% filter( S == "b"),
aes(fill = groups), stat = "identity", position = "dodge") +
geom_bar(data = . %>% filter( S == "a"),
aes(group = groups),
fill = "grey80", stat = "identity", position = "dodge")


You can adjust fill = "grey80" to get the exact colour you'd like.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 30 '18 at 9:07









ConstantinosConstantinos

813411




813411













  • This is exactly what I was looking for! Thank you!

    – user13874
    Dec 31 '18 at 22:45



















  • This is exactly what I was looking for! Thank you!

    – user13874
    Dec 31 '18 at 22:45

















This is exactly what I was looking for! Thank you!

– user13874
Dec 31 '18 at 22:45





This is exactly what I was looking for! Thank you!

– user13874
Dec 31 '18 at 22:45


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53880175%2fggplot-desaturate-make-gray-bars-based-on-column-value%23new-answer', 'question_page');
}
);

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







F0sOrqDSx LoP4LznwgZo wWVvd7
j8YTVmylL V6imH6pC0,rtbEZ0R,HhzbD25n6dMhAk7aJrBDlU1MekkJicFdLqqV3 Qd,7Y8OudDarOfIO

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas