我正在尝试在base R
中绘制数据点在顶部的条形图。
我使用base R
是因为无法以简单的方式创建纹理来填充ggplot
(例如,请参见here,并且ggtexture不允许进行复杂的编辑)。
使用barplot()
功能和points()
,我可以做到这一点:
library(tidyverse)
#Sample data
data <- iris %>%
group_by(Species) %>%
summarise(m = mean(Sepal.Length),
se = sd(Sepal.Length)/
sqrt(sum(!is.na(Sepal.Length)))) %>%
ungroup()
chart <- barplot(height=data$m, names=data$Species,
density = c(5, 5, 5),
angle = c(0,45,90),
col = "brown",
width = c(0.1,0.1,0.1),
font.axis = 2,
border = c("black"),
lwd = 2)
points(x = chart,
y = data$m)
但是,我想创建类似于以下内容:
iris %>%
group_by(Species) %>%
summarise(m = mean(Sepal.Length),
se = sd(Sepal.Length)/
sqrt(sum(!is.na(Sepal.Length)))) %>%
ungroup() %>%
ggplot(aes(Species, m,
group = Species,
color = Species,
shape = Species)) +
geom_bar(stat = "identity", fill="white",
color="black") +
geom_jitter(
aes(Species, Sepal.Length),
data = iris)
您可以在此处使用jitter
功能。
chart <- barplot(height=data$m, names=data$Species,
density = c(5, 5, 5),
angle = c(0,45,90),
col = "brown",
width = c(0.1,0.1,0.1),
font.axis = 2,
border = c("black"),
lwd = 2, las=1,
ylim=c(0,8))
points(x = lapply(rep(chart, each=50), jitter, amount=0.05),
y = iris$Sepal.Length,
col=iris$Species, pch=20)