使用ggplot2包向“geom_bar”添加图例

时间:2022-11-26 14:58:04

I am a newbie to R, so please pardon my ignorance. I made a pseudo-stacked barplot in which I drew 4 sets of bars on top of each other using geom_bar. There are 4 health status categories (alive, dead, infected, & sod-dead) for three species of oak trees (QUAG, QUKE, QUCH).

我是新手,所以请原谅我的无知。我做了一个伪堆叠的条形图,我在上面画了4组条形图,用的是土地线。三种橡树(QUAG, QUKE, QUCH)有4种健康状态类别(存活、死亡、感染和死亡)。

My code is as follows:

我的代码如下:


x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), alive = c(627,208,109),  infected = c(102,27,0), dead = c(133,112,12), sod.dead=c(49,8,0)))

x.plot = ggplot(x, aes(variable, alive)) + geom_bar(fill="gray85") + 
  geom_bar(aes(variable,dead), fill="gray65") +
  geom_bar(aes(variable, infected), fill="gray38") +
  geom_bar(aes(variable, sod.dead), fill="black")+
  opts(panel.background = theme_rect(fill='gray100'))
x.plot

Now I want to make a legend that shows which shade of gray relates to tree status, i.e., "gray65" is "dead trees", etc. I've been trying for the past hour and can't get it to work.

现在我想做一个图例来说明灰色的阴影与树的状态之间的关系。“gray65”是“死树”等等。我已经试了一个小时了,但还是没能让它正常工作。

2 个解决方案

#1


9  

I see that @Brandon Bertelsen has posted a great answer. I would like to add some code that addresses additional details mentioned in the original post:

我看到@Brandon Bertelsen发布了一个很棒的答案。我想添加一些代码,以解决原始帖子中提到的其他细节:

  1. After you reshape your data and map health status to fill, ggplot will create the legend automatically.
  2. 在您重塑您的数据并映射健康状态以填充之后,ggplot将自动创建传奇。
  3. I suggest using scale_fill_manual() to get the exact grays mentioned in the original post.
  4. 我建议使用scale_fill_manual()来获取原始文章中提到的确切灰色。
  5. theme_bw() is a handy function to quickly get a black and white look to your plot.
  6. theme_bw()是一个方便的函数,可以快速地使您的情节呈现黑白。
  7. The plotting order of factor levels/colors can be controlled by specifying the desired order with the levels argument of factor().
  8. 可以通过使用factor()的level参数指定所需的顺序来控制因子级别/颜色的绘制顺序。
  9. A dodged barplot (instead of stacked) may have some advantages for this data set.
  10. 对于这个数据集来说,一个被避开的barplot(而不是堆叠)可能有一些优势。

library(reshape2)
library(ggplot2)

x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), 
                        alive=c(627, 208, 109),  infected=c(102, 27, 0), 
                        dead=c(133, 112, 12), sod.dead=c(49, 8, 0)))

# Put data into 'long form' with melt from the reshape2 package.
dat = melt(x, id.var="variable", variable.name="status")

head(dat)
#    variable   status value
# 1      QUAG    alive   627
# 2      QUKE    alive   208
# 3      QUCH    alive   109
# 4      QUAG infected   102
# 5      QUKE infected    27
# 6      QUCH infected     0

# By manually specifying the levels in the factor, you can control
# the stacking order of the associated fill colors.
dat$status = factor(as.character(dat$status), 
                    levels=c("sod.dead", "dead", "infected", "alive"))

# Create a named character vector that relates factor levels to colors.
grays = c(alive="gray85", dead="gray65", infected="gray38", sod.dead="black")

plot_1 = ggplot(dat, aes(x=variable, y=value, fill=status)) +
         theme_bw() +
         geom_bar(position="stack") +
         scale_fill_manual(values=grays)

ggsave(plot=plot_1, filename="plot_1.png", height=5, width=5)

使用ggplot2包向“geom_bar”添加图例

# You may also want to try a dodged barplot.
plot_2 = ggplot(dat, aes(x=variable, y=value, fill=status)) +
         theme_bw() +
         geom_bar(position="dodge") +
         scale_fill_manual(values=grays)

ggsave(plot=plot_2, filename="plot_2.png", height=4, width=5)

使用ggplot2包向“geom_bar”添加图例

#2


2  

You need to reshape your data.

你需要重塑你的数据。

library(reshape)
library(ggplot2)

x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), alive = c(627,208,109),  infected = c(102,27,0), dead = c(133,112,12), sod.dead=c(49,8,0)))

x <- melt(x)
colnames(x) <- c("Type","Status","value")

ggplot(x, aes(Type, value, fill=Status)) + geom_bar(position="stack")

#1


9  

I see that @Brandon Bertelsen has posted a great answer. I would like to add some code that addresses additional details mentioned in the original post:

我看到@Brandon Bertelsen发布了一个很棒的答案。我想添加一些代码,以解决原始帖子中提到的其他细节:

  1. After you reshape your data and map health status to fill, ggplot will create the legend automatically.
  2. 在您重塑您的数据并映射健康状态以填充之后,ggplot将自动创建传奇。
  3. I suggest using scale_fill_manual() to get the exact grays mentioned in the original post.
  4. 我建议使用scale_fill_manual()来获取原始文章中提到的确切灰色。
  5. theme_bw() is a handy function to quickly get a black and white look to your plot.
  6. theme_bw()是一个方便的函数,可以快速地使您的情节呈现黑白。
  7. The plotting order of factor levels/colors can be controlled by specifying the desired order with the levels argument of factor().
  8. 可以通过使用factor()的level参数指定所需的顺序来控制因子级别/颜色的绘制顺序。
  9. A dodged barplot (instead of stacked) may have some advantages for this data set.
  10. 对于这个数据集来说,一个被避开的barplot(而不是堆叠)可能有一些优势。

library(reshape2)
library(ggplot2)

x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), 
                        alive=c(627, 208, 109),  infected=c(102, 27, 0), 
                        dead=c(133, 112, 12), sod.dead=c(49, 8, 0)))

# Put data into 'long form' with melt from the reshape2 package.
dat = melt(x, id.var="variable", variable.name="status")

head(dat)
#    variable   status value
# 1      QUAG    alive   627
# 2      QUKE    alive   208
# 3      QUCH    alive   109
# 4      QUAG infected   102
# 5      QUKE infected    27
# 6      QUCH infected     0

# By manually specifying the levels in the factor, you can control
# the stacking order of the associated fill colors.
dat$status = factor(as.character(dat$status), 
                    levels=c("sod.dead", "dead", "infected", "alive"))

# Create a named character vector that relates factor levels to colors.
grays = c(alive="gray85", dead="gray65", infected="gray38", sod.dead="black")

plot_1 = ggplot(dat, aes(x=variable, y=value, fill=status)) +
         theme_bw() +
         geom_bar(position="stack") +
         scale_fill_manual(values=grays)

ggsave(plot=plot_1, filename="plot_1.png", height=5, width=5)

使用ggplot2包向“geom_bar”添加图例

# You may also want to try a dodged barplot.
plot_2 = ggplot(dat, aes(x=variable, y=value, fill=status)) +
         theme_bw() +
         geom_bar(position="dodge") +
         scale_fill_manual(values=grays)

ggsave(plot=plot_2, filename="plot_2.png", height=4, width=5)

使用ggplot2包向“geom_bar”添加图例

#2


2  

You need to reshape your data.

你需要重塑你的数据。

library(reshape)
library(ggplot2)

x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), alive = c(627,208,109),  infected = c(102,27,0), dead = c(133,112,12), sod.dead=c(49,8,0)))

x <- melt(x)
colnames(x) <- c("Type","Status","value")

ggplot(x, aes(Type, value, fill=Status)) + geom_bar(position="stack")