使用geom_bar为不同的facet_grids填充不同的填充

时间:2023-02-09 20:26:54

I'm new with ggplot2 and I have a question that I couldn't find the answer. I've created the following toy data to help in the explanation:

我是ggplot2的新手,我有一个问题,我无法找到答案。我创建了以下玩具数据以帮助解释:

data <- data.frame(tech=c(rep(letters[1:15],2)), 
     sep=c(rep(c("SitutationA", "SitutationB"),each=15)), 
     error=c(runif(15,min=-0.2, max=0.5), runif(15, min=0.3, max=1)))

I want to plot a geom_bar graph showing the "error" (axis y) for each technique "tech" (axis x) divided in two different situations (SituationA and SituationB) using facet_grid. The color (fill) of each bar should represent the "error" of each technique, and not the technique (as a factor). The errors for situations A and B are measured in different scales. However, in my code, an error of the same value have the same color in both situations. I do not want this behavior since they were measured in different scales. Thus, I would like that the colors in Situations A and B were independents.

我想绘制一个geom_bar图,显示每个技术“tech”(轴x)的“错误”(轴y),使用facet_grid分为两种不同的情况(SituationA和SituationB)。每个条形的颜色(填充)应代表每种技术的“误差”,而不是技术(作为一个因素)。情况A和B的误差以不同的比例测量。但是,在我的代码中,相同值的错误在两种情况下都具有相同的颜色。我不想要这种行为,因为它们是以不同的比例测量的。因此,我希望情况A和B中的颜色是独立的。

The following code plots the graph, but using the same color for both situations.

以下代码绘制了图形,但在两种情况下使用相同的颜色。

ggplot(data, aes(x=tech, y=error)) +
  geom_bar(aes(fill=error), stat="identity", position="dodge") + 
  facet_grid(sep ~ ., scales="free_y") + 
  scale_fill_continuous(guide=FALSE)

How could I use different continuous fills for each facet (situationA and situationB)?

我怎样才能为每个方面使用不同的连续填充(situationA和situationB)?

Thank you.

谢谢。

1 个解决方案

#1


1  

You can't have two different fill scales on the same plot.

在同一个图上不能有两个不同的填充比例。

Solution to the problem could be to make two plots and then put them together with grid.arrange() from library gridExtra.

该问题的解决方案可能是制作两个图,然后将它们与来自库gridExtra的grid.arrange()放在一起。

In the first plot put only values of SitutationA. Changed y scale to show values with two numbers after decimal point (to be the same as for second plot). Removed x axis title, texts and ticks and changed plot margins - set bottom margin to -0.4 to reduce space between plots.

在第一个图中只放置了SitutationA的值。更改y比例以显示小数点后两位数的值(与第二个绘图相同)。删除了x轴标题,文本和刻度以及更改了绘图边距 - 将底部边距设置为-0.4以减少绘图之间的间距。

library(grid)
library(gridExtra)
p1<-ggplot(subset(data,sep=="SitutationA"), aes(x=tech, y=error)) +
  geom_bar(aes(fill=error), stat="identity", position="dodge") + 
  facet_grid(sep ~ ., scales="free_y") + 
  scale_fill_continuous(guide=FALSE)+
  scale_y_continuous(breaks=c(0,0.25,0.50))+
  theme(axis.text.x=element_blank(),
        axis.title.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,-0.4,1),"lines"))

For the second plot (SitutationB) changed top plot margin to -0.4 to reduce space between plots. Then changed scale_fill_continuous() and provided new colors.

对于第二个绘图(SitutationB),将顶部绘图边距更改为-0.4以减少绘图之间的间距。然后改变了scale_fill_continuous()并提供了新的颜色。

p2<-ggplot(subset(data,sep=="SitutationB"), aes(x=tech, y=error)) +
  geom_bar(aes(fill=error), stat="identity", position="dodge") + 
  facet_grid(sep ~ ., scales="free_y") + 
  scale_fill_continuous(guide=FALSE,low="red",high="purple") +
  theme(plot.margin=unit(c(-0.4,1,1,1),"lines"))

Now put both plots together.

现在把两个地块放在一起。

grid.arrange(p1,p2)

使用geom_bar为不同的facet_grids填充不同的填充

#1


1  

You can't have two different fill scales on the same plot.

在同一个图上不能有两个不同的填充比例。

Solution to the problem could be to make two plots and then put them together with grid.arrange() from library gridExtra.

该问题的解决方案可能是制作两个图,然后将它们与来自库gridExtra的grid.arrange()放在一起。

In the first plot put only values of SitutationA. Changed y scale to show values with two numbers after decimal point (to be the same as for second plot). Removed x axis title, texts and ticks and changed plot margins - set bottom margin to -0.4 to reduce space between plots.

在第一个图中只放置了SitutationA的值。更改y比例以显示小数点后两位数的值(与第二个绘图相同)。删除了x轴标题,文本和刻度以及更改了绘图边距 - 将底部边距设置为-0.4以减少绘图之间的间距。

library(grid)
library(gridExtra)
p1<-ggplot(subset(data,sep=="SitutationA"), aes(x=tech, y=error)) +
  geom_bar(aes(fill=error), stat="identity", position="dodge") + 
  facet_grid(sep ~ ., scales="free_y") + 
  scale_fill_continuous(guide=FALSE)+
  scale_y_continuous(breaks=c(0,0.25,0.50))+
  theme(axis.text.x=element_blank(),
        axis.title.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,-0.4,1),"lines"))

For the second plot (SitutationB) changed top plot margin to -0.4 to reduce space between plots. Then changed scale_fill_continuous() and provided new colors.

对于第二个绘图(SitutationB),将顶部绘图边距更改为-0.4以减少绘图之间的间距。然后改变了scale_fill_continuous()并提供了新的颜色。

p2<-ggplot(subset(data,sep=="SitutationB"), aes(x=tech, y=error)) +
  geom_bar(aes(fill=error), stat="identity", position="dodge") + 
  facet_grid(sep ~ ., scales="free_y") + 
  scale_fill_continuous(guide=FALSE,low="red",high="purple") +
  theme(plot.margin=unit(c(-0.4,1,1,1),"lines"))

Now put both plots together.

现在把两个地块放在一起。

grid.arrange(p1,p2)

使用geom_bar为不同的facet_grids填充不同的填充