I want to highlight "Prime" element only in the bar plot. However, when there is no "Prime" element in particular column, this bar plot highlight all the bar (which it should not be highlighted).
This is my R code for further information:
test.df <- data.frame(replace_punct = c("Prime", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured"), pos = c("Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured"), spelling_corr = c("Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Prime", "Prime"), stringsAsFactors = FALSE)
replace_punct_bar <- ggplot(data=test.df, aes(x = replace_punct, y = after_stat(count/sum(count)), fill = ifelse(replace_punct == "Prime", "Highlighted", "Normal"))) scale_y_continuous(labels = scales::percent) geom_bar() labs(title = "Replace punctuation freq", y = "Percent", x = "Status") geom_hline(aes(yintercept=0.6), colour="#990000", linetype="dashed") theme(legend.position="none")
pos_bar <- ggplot(data=test.df, aes(x = pos, y = after_stat(count/sum(count)), fill = ifelse(pos == "Prime", "Highlighted", "Normal"))) scale_y_continuous(labels = scales::percent) geom_bar() labs(title = "Part of speech freq", y = "Percent", x = "Status") geom_hline(aes(yintercept=0.6), colour="#990000", linetype="dashed") theme(legend.position="none")
spelling_corr_bar <- ggplot(data=test.df, aes(x = spelling_corr, y = after_stat(count/sum(count)), fill = ifelse(spelling_corr == "Prime", "Highlighted", "Normal"))) scale_y_continuous(labels = scales::percent) geom_bar() labs(title = "Spelling correction freq", y = "Percent", x = "Status") geom_hline(aes(yintercept=0.6), colour="#990000", linetype="dashed") theme(legend.position="none")
grid.arrange(replace_punct_bar, pos_bar, spelling_corr_bar, ncol=3)
Thank you in advance for your time and help!
CodePudding user response:
You can add a scale, like:
scale_fill_manual(values = c("Highlighted" = "purple","Not Normal" = "grey"))
in every plot, the result would be:
CodePudding user response:
I got the idea from @vinícius-félix answer. Here is the updated code on how the bar plot that I wanted it to be visualized.
test.df <- data.frame(replace_punct = c("Prime", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured"), pos = c("Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured"), spelling_corr = c("Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Not Measured", "Prime", "Prime"), stringsAsFactors = FALSE)
replace_punct_bar <- ggplot(data=test.df, aes(x = replace_punct, y = after_stat(count/sum(count)), fill = ifelse(replace_punct == "Prime", "Highlighted", "Normal"))) scale_y_continuous(labels = scales::percent) geom_bar() labs(title = "Replace punctuation freq", y = "Percent", x = "Status") geom_hline(aes(yintercept=0.6), colour="#990000", linetype="dashed") theme(legend.position="none") scale_fill_manual(values = c("Highlighted" = "#f8766d","Normal" = "#00bfc4"))
pos_bar <- ggplot(data=test.df, aes(x = pos, y = after_stat(count/sum(count)), fill = ifelse(pos == "Prime", "Highlighted", "Normal"))) scale_y_continuous(labels = scales::percent) geom_bar() labs(title = "Part of speech freq", y = "Percent", x = "Status") geom_hline(aes(yintercept=0.6), colour="#990000", linetype="dashed") theme(legend.position="none") scale_fill_manual(values = c("Highlighted" = "#f8766d","Normal" = "#00bfc4"))
spelling_corr_bar <- ggplot(data=test.df, aes(x = spelling_corr, y = after_stat(count/sum(count)), fill = ifelse(spelling_corr == "Prime", "Highlighted", "Normal"))) scale_y_continuous(labels = scales::percent) geom_bar() labs(title = "Spelling correction freq", y = "Percent", x = "Status") geom_hline(aes(yintercept=0.6), colour="#990000", linetype="dashed") theme(legend.position="none") scale_fill_manual(values = c("Highlighted" = "#f8766d","Normal" = "#00bfc4"))
grid.arrange(replace_punct_bar, pos_bar, spelling_corr_bar, ncol=3)
I hope this code help anybody that facing the problem like mine. Thanks!