使用不同的变量绘制geom_bar和geom_point来绘制颜色/填充点(分组图)

时间:2020-06-23 23:31:37

标签: r ggplot2

我已经搜索了帖子,但找不到帮助我解决问题的具体示例。 (这是我的第一篇文章,所以我不能直接包含图像,但是我包含了一个链接,该代码应该可以说明我的问题。)

我正在尝试在ggplot中制作一个分组图,其中有一个条形图显示平均值,然后是各个点显示各个重复项。如果不根据特定的组来更改点的形状,我就可以使其正常工作。

但是,我想通过复制来塑造点。当我尝试执行此操作时,geom_point似乎将事物分组不正确,并且这些点不再与条形图中显示的数据相对应。

这是一些示例数据。有4种水果来源,4种不同类型的水果,每种来源/水果组合都有2个重复。

values <- runif(n = 32, min = 50, max = 100)
source <- rep(c("grocery", "garden", "market", "farm"), each = 8)
replicate <- rep(c("A", "B"), times = 16)
fruit <- rep(c("apple", "orange", "banana", "grape"), each = 2, times = 4)

df <- data.frame(source, fruit, replicate, values)

# change fruit to factor
df$fruit <- factor(df$fruit, levels = c("apple", "orange", "banana", "grape"))

现在,我制作一个条形图,该图将两个重复的平均值绘制成图表,并按四个来源将四个水果分组在一起。

g <- ggplot(data = df, aes(x = source, y = values, fill = fruit)) +
  geom_bar(position = "dodge", color = "black", stat = "summary", fun = "mean") +
  scale_fill_manual(values = c("gray65", "deepskyblue1", "orange", "olivedrab4")) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position = "top",
        legend.title = element_text(face = "bold", size = 10),
        legend.text = element_text(size = 10),
        strip.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(size=.5, linetype="solid", color = "black"))

g

bar graph

接下来,我添加点以显示两个重复项。效果很好。

#add in geom_point to show duplicates

g + geom_point(position = position_dodge(width = 0.9))

bar graph + points

但是,当我尝试为两个重复项添加不同的形状时,现在它将重复项分组在一起,而不是在每个点上都保持重复项。因此,这些点与条形图不正确对应。我尝试在第一行ggplot行中向aes添加“ shape =复制”,但这导致每个复制都有单独的条形图。我确定我已经接近了,但是不知道还能尝试什么。

#make the shape of the points correspond to the celltype

g + geom_point(aes(shape = replicate), position = position_dodge(width = 0.9))

bar graph + points showing replicate (NOT working)

1 个答案:

答案 0 :(得分:0)

添加shape = replicate会使position_dodge变得混乱,因为然后闪避既适用于fruit也适用于replciate。为了克服这个问题,我的方法将单独的geom_point层用于复制。试试这个:

set.seed(42)
values <- runif(n = 32, min = 50, max = 100)
source <- rep(c("grocery", "garden", "market", "farm"), each = 8)
replicate <- rep(c("A", "B"), times = 16)
fruit <- rep(c("apple", "orange", "banana", "grape"), each = 2, times = 4)

df <- data.frame(source, fruit, replicate, values)

# change fruit to factor
df$fruit <- factor(df$fruit, levels = c("apple", "orange", "banana", "grape"))

library(ggplot2)
library(dplyr)

g <- ggplot(data = df, aes(x = source, y = values, fill = fruit)) +
  geom_bar(position = "dodge", color = "black", stat = "summary", fun = "mean") +
  scale_fill_manual(values = c("gray65", "deepskyblue1", "orange", "olivedrab4")) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position = "top",
        legend.title = element_text(face = "bold", size = 10),
        legend.text = element_text(size = 10),
        strip.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(size=.5, linetype="solid", color = "black"))

g + 
  geom_point(data = filter(df, replicate == "A"), aes(shape = "A"), position = position_dodge(width = 0.9)) +
  geom_point(data = filter(df, replicate == "B"), aes(shape = "B"), position = position_dodge(width = 0.9)) +
  guides(fill = guide_legend(override.aes = list(shape = c(NA, NA, NA, NA))))