【问题标题】:coloring specific bars in grouped barplot为分组条形图中的特定条着色
【发布时间】:2016-01-31 03:42:42
【问题描述】:

我正在尝试绘制分组条形图。我想为 >=1 的条着色,并将其余的条保留为未填充。我该怎么做?

这是我的代码

data = read.csv ("/home/paul/Desktop/dataset.csv")  
library(reshape2)
library(ggplot2)
df.long<-melt(data)
df.long$names <- factor(df.long$names, levels=unique(df.long$names))
ggplot(df.long,aes(x=names,y=value,fill=variable))+ labs(x = "x", y = "y" ) + ylim(0, 2)  + 
geom_bar(stat="identity",position="dodge") + 
scale_fill_hue(l=40) + 
theme(axis.text=element_text(size=14), axis.title=element_text(size=16,face="bold"))

我的数据

names   d1  d2
E1  1.30    1.27
K2  1.05    1.86
D4  0.94    1.51
E2  1.01    1.62
N1  1.17    1.47
Q3  1.22    1.51
S7  1.00    1.24
G2  0.78    0.96
H5  1.04    1.04
T1  1.04    1.14
A5  0.71    0.71
P4  1.03    1.27
Y2  1.34    0.58
V4  0.83    0.50
M3  1.02    0.53
C7  0.98    0.31

【问题讨论】:

  • 添加一个变量 isColored 到您的数据框中,如果您想为项目着色,该变量 == 1,并在您的 aes 中使用 color = isColoredfill = isColored

标签: r plot ggplot2


【解决方案1】:

您可以在aes()fill 参数中指定填充标准,并将不想填充的手动填充颜色设置为NA

library(ggplot2)
set.seed(123)
df <- data.frame(names = letters[1:10],
                 d1 = runif(10, 0, 2),
                 d2 = runif(10, 0, 2))

## using a manual scale
ggplot(data=df, aes(x=names, y=d1, fill=(d1 >= 1))) +
  geom_bar(stat="identity") + 
  scale_fill_manual(values = c(NA, "steelblue"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多