1

我有以下数据集:

cond <- gl(15,1,labels=c("a1","a2","a3","a4","a5","b1","b2","b3","b4","b5","c1","c2","c3","c4","c5"))
pos <-c(rep("a",5),rep("b",5),rep("c",5))
mean <- c(3.202634, 3.819009, 3.287785, 4.531127, 3.093865, 3.360535, 4.084791, 3.886960, 3.297692, 4.281323, 2.418745, 3.759699, 3.553860, 4.812989, 1.606597)
hd <- c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE)

df <- data.frame(cond,pos,mean,hd)

...并生成了这个情节

library(ggplot2)
b <- ggplot(df, aes(x=cond, y = mean, fill=pos))  + labs(x = "X", y="Y", fill=NULL)
c <- b + geom_bar(stat = "identity", position="dodge") + theme(text = element_text(size=18), axis.text.x = element_text(colour="black", size = 14)) + scale_fill_brewer(palette="Set1") 

my_theme <- theme_update(panel.grid.major = element_line(colour = "grey90"), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.ticks = element_blank(), legend.position = "none") 

在此处输入图像描述

现在我想根据数据框的hd列调整颜色,以便条形图的每一列都获得相应颜色的稍暗阴影(例如深红色而不是红色)hd=="TRUE"

我怎样才能做到这一点?

还有一个问题:如何增加labsX 和 Y 与绘图/轴之间的距离?

4

1 回答 1

7

您可以使用interaction()内部函数aes()来提供 fill= 值作为hd和的组合pos。然后scale_fill_manual()提供颜色。

ggplot(df, aes(x=cond, y = mean, fill=interaction(hd,pos)))  + 
  labs(x = "X", y="Y", fill=NULL)+
  geom_bar(stat = "identity", position="dodge") + 
  scale_fill_manual(values=c("red","darkred","blue","darkblue","green","darkgreen")) 

要更改轴标题的位置,请在函数内部同时theme()使用axis.title=axis.title.x/axis.title.y分别用于 x/y 轴标题,然后vjust=. 0 及以下的值会使标题降低。为确保标签有位置,您可以增加绘图边距plot.margin=(您必须添加library(grid)使用功能单元)。

library(grid)
your.plot + theme(plot.margin=unit(c(0.5,0.5,2,2),"cm"))+
            theme(axis.title=element_text(vjust=0))

在此处输入图像描述

于 2013-08-31T15:31:57.603 回答