我已经搜索了帖子,但找不到可以帮助我解决问题的具体示例。(这是我的第一篇文章,所以我不能直接包含图像,但是我包含了一个链接,该代码应该可以说明我的问题。)
我试图在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
接下来,我添加点以显示两个副本。这很好。
#add in geom_point to show duplicates
g + geom_point(position = position_dodge(width = 0.9))
但是,当我尝试为两个复制品添加不同的形状时,现在它将复制品分组在一起,而不是在每个点上都保留复制品。因此,这些点与条形图不正确对应。我尝试在第一行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))
添加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))))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句