我有下图,我想突出显示两个西瓜的列,因为它的juice_content和重量最高。我知道如何更改列的颜色,但我想突出显示整个列。关于如何实现这一点的任何想法?网上似乎没有任何类似的内容。
fruits <- c("apple","orange","watermelons")
juice_content <- c(10,1,1000)
weight <- c(5,2,2000)
df <- data.frame(fruits,juice_content,weight)
df <- gather(df,compare,measure,juice_content:weight, factor_key=TRUE)
plot <- ggplot(df, aes(fruits,measure, fill=compare)) + geom_bar(stat="identity", position=position_dodge()) + scale_y_log10()
Run Code Online (Sandbox Code Playgroud)
一种选择是使用 gghighlight
library(gghighlight)
ggplot(df, aes(fruits,measure, fill = compare)) +
geom_col(position = position_dodge()) +
scale_y_log10() +
gghighlight(fruits == "watermelons")
Run Code Online (Sandbox Code Playgroud)
根据您的评论,如何使用不同的alpha
价值观
ggplot(df, aes(fruits,measure)) +
geom_col(data = . %>% filter(fruits == "watermelons"),
mapping = aes(fill = compare),
position = position_dodge()) +
geom_col(data = . %>% filter(fruits != "watermelons"),
mapping = aes(fill = compare),
alpha = 0.2,
position = position_dodge()) +
scale_y_log10()
Run Code Online (Sandbox Code Playgroud)
或者,您也可以使用一个geom_col
和一个条件来实现相同的效果alpha
(感谢@Tjebo)
ggplot(df, aes(fruits, measure)) +
geom_col(
mapping = aes(fill = compare, alpha = fruits == 'watermelons'),
position = position_dodge()) +
scale_alpha_manual(values = c(0.2, 1)) +
scale_y_log10()
Run Code Online (Sandbox Code Playgroud)