带有数据点的barplot()-基数R

问题描述 投票:0回答:1

我正在尝试在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)

enter image description here

但是,我想创建类似于以下内容:

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) 

enter image description here

r plot bar-chart
1个回答
0
投票

您可以在此处使用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)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.