r - 在 ggplot 中突出显示单个 "bar"

标签 r ggplot2 bar-chart

我想为条形图中的各个条选择颜色和填充图案(突出显示单个“条”)

我想用另一种颜色填充的条形是“Str”和“RB”
enter image description here

ggplot(GZ.mean, aes(x=Group.1, y=B)) +theme_bw(base_size=20, base_family="Times")+
geom_bar(stat="identity",colour="black", width=.6, position = "dodge", ,fill="gainsboro") +geom_errorbar(my.limits, width=0.2) +
theme(axis.text.x=element_text(family="Times", colour="black", size=rel(1.2), angle=30, hjust=1, vjust=1))+
theme(axis.text.y=element_text(family="Times", colour="black", size=rel(1.2))) +   scale_y_continuous(limits=c(0,170))+geom_text(size=6,aes(label=c("a","d","c","e","b","d","d","b","bc","d", "bc"),hjust=offset.h, vjust=offset.v)) +
scale_x_discrete(limits=c("JdC", "Stu", "Str", "Bol", "WBr", "Rij4", "Bif", "ErL", "ZtG", "PdV", "RB")) +labs(x= "Variety", y= "Total Sugar concentration [mg * g-1 FW]")

我已经尝试过“scale_fill_manual”和“scale_color_manual”,但它仍然不起作用。

最佳答案

scale_fill_manual应该管用。在您的数据中创建一列指示是否应突出显示该条,然后将该列提供给 fill审美的。 scale_fill_manual然后可用于根据需要分配颜色。下面是一个有代表性的例子:

library( tidyverse )
library( ggplot2 )

## Generate some data to plot
X <- mtcars %>% group_by( cyl ) %>% summarize( mpg = mean(mpg) ) %>% ungroup

## Add a column indicating whether the category should be highlighted
X <- X %>% mutate( ToHighlight = ifelse( cyl == 6, "yes", "no" ) )

## Data looks like this
## A tibble: 3 x 3
##    cyl      mpg ToHighlight
##  <dbl>    <dbl>       <chr>
##1     4 26.66364          no
##2     6 19.74286         yes
##3     8 15.10000          no

## Plot the data
ggplot( X, aes( x = cyl, y = mpg, fill = ToHighlight ) ) +
    geom_bar( stat = "identity" ) +
    scale_fill_manual( values = c( "yes"="tomato", "no"="gray" ), guide = FALSE )

备注 guide = FALSE隐藏与 ToHighlight 相关联的图例柱子。

enter image description here

关于r - 在 ggplot 中突出显示单个 "bar",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45820250/

相关文章:

r - 按组划分的真/假值计数条形图(斜线图)

ios - 当用户点击图表时创建一个 MarkerView

r - 来自 R 的 C 编译标志

r - 加入两个具有最接近时间戳间隔的数据集

r - 如何在R中动态更改图表标题?

r - 使用列作为在 R 中绘图的因素

r - 如何让 R 接受自签名证书

r - 将 R_markdown 编织成 PDF 时加载命名空间出错

r - 使用 Plotly 和 R 的悬停模式

r - 为条形图定义一个固定的宽度/高度,然后设置条形的绝对宽度和条形之间的绝对间距,以像素为单位