【问题标题】:Changing data point shape of second y-axis with ggplot用ggplot改变第二个y轴的数据点形状
【发布时间】:2020-02-17 18:56:39
【问题描述】:

在我的示例中,我在 ggplot 中创建了第二个 y 轴。 我不想改变第二个 y 轴数据点的颜色,而是改变形状。 但是,当然,我得到了错误Scale for 'shape' is already present. Adding another scale for 'shape', which will replace the existing scale. 最后,我想为第二个 y 轴的“访问”设置一个“大”圆圈和一个“小”实心圆圈。

这是我的代码:

Treatment <- c(rep(c("T/T"), times = 6),rep(c("R/R"), times = 6))
Size <- c(rep(c("Large", "Large", "Large", "Small", "Small", "Small"), times = 2))
Areatime <- c(240, 220, 120, 60, 55, 75, 90, 45, 70, 115, 40, 30)
Visits <- c(30, 45, 45, 65, 55, 55, 25, 35, 45, 75, 65, 55)
Counts <- data.frame(Treatment, Size, Areatime, Visits)

Counts$Treatment <- factor(Counts$Treatment, levels=c("T/T","R/R"))
Counts$inter <- interaction(Counts$Treatment, Counts$Size)
str(Counts)

ggplot(Count, aes(x = Treatment, y = Areatime)) +
  geom_jitter(aes(y = Areatime, shape = Size, width = 0.3)) +
  scale_shape_manual(values = c(0, 15), name = "Areatime", breaks = c("Large", "Small"),
    labels = c("Large", "Small")) +
  geom_jitter(aes(y = Counts*3, colour = Size, width = 0.3)) +
  scale_y_continuous(sec.axis = sec_axis(~./3, name = "Relative Visits [%]"))+
  scale_colour_manual(values =c(1, 2), name="Visits", breaks=c("Large", "Small"),
                     labels=c("Large", "Small")) +
  theme(axis.text.x=element_blank(),
                 panel.grid.major = element_blank(),
                 panel.grid.minor = element_blank(),
                 axis.ticks.x = element_blank(),
                 axis.line.x = element_blank(),
                 axis.line.y = element_line("black")) +
  facet_grid(.~Treatment, scales="free", switch = "x") +
  labs(y = "Average Areatime [sec]")

Plot of Example

【问题讨论】:

标签: r ggplot2


【解决方案1】:

我玩了一下,这不是一个优雅的 hack,但也许它已经足够好了。您可以使用shape = 21 这是一个可填充的形状。然后您需要将填充颜色与背景颜色匹配,使其看起来没有填充。您的示例代码中有一些错字。我希望我正确地修复了它们。

Treatment <- c(rep(c("T/T"), times = 6),rep(c("R/R"), times = 6)) 
Size <- c(rep(c("Large", "Large", "Large", "Small", "Small", "Small"), times = 2)) 
Areatime <- c(240, 220, 120, 60, 55, 75, 90, 45, 70, 115, 40, 30) Visits <- c(30, 45, 45, 65, 55, 55, 25, 35, 45, 75, 65, 55) 

Counts <- data.frame(Treatment, Size, Areatime, Visits)

Counts$Treatment <- factor(Counts$Treatment, levels=c("T/T","R/R")) 
Counts$inter <- interaction(Counts$Treatment, Counts$Size) str(Counts)

ggplot(data = Counts, 
       aes(x = Treatment, 
           y = Areatime)) +   geom_jitter(aes(y = Areatime, 
                  colour = Size,
                  shape = Size),
              size = 3,
              width = 0.3) +   scale_shape_manual(values = c(0, 15),
                     name = "Areatime",
                     breaks = c("Large", "Small"),
                     labels = c("Large", "Small")) +   scale_colour_manual(values = c(1, 2)) +   geom_jitter(aes(y = Visits, 
                  fill = Size), 
              shape = 21, # this shape we can fill, but will give a border
              size = 3,
              width = 0.3) +   scale_y_continuous(sec.axis = sec_axis(~./3, name = "Relative Visits [%]"))+   scale_fill_manual(values = c("grey95", "red"),
                    name = "Visits",
                    breaks = c("Large", "Small"),
                    labels = c("Large", "Small")) +   guides(colour = guide_legend(title = "Areatime")) +   theme(axis.text.x=element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank(),
        axis.line.y = element_line("black")) +   facet_grid(. ~Treatment, 
             scales="free", 
             switch = "x") +   labs(y = "Average Areatime [sec]")

这同时产生:

【讨论】:

    【解决方案2】:

    太好了,谢谢!我只是将两者的颜色更改为黑色而不是红色,并将背景设置为相同的灰色,并在“访问”之后的第二个 geom_jitter 中添加了 *3(您只是因为我的错字而忘记了)。所以,解决办法是:

    ggplot(data = Counts, aes(x = Treatment, y = Areatime)) +
      geom_jitter(aes(y = Areatime, colour = Size, shape = Size), size = 3, width = 0.3) +
      scale_shape_manual(values = c(0, 15), name = "Areatime",
                         breaks = c("Large", "Small"), labels = c("Large", "Small")) +
      scale_colour_manual(values = c(1, 1)) +
      geom_jitter(aes(y = Visits*3, fill = Size), shape = 21, size = 3, width = 0.3) +
      scale_y_continuous(sec.axis = sec_axis(~./3, name = "Relative Visits [%]")) +
      scale_fill_manual(values = c("gray95", "black"),name = "Visits",
                        breaks = c("Large", "Small"), labels = c("Large", "Small")) +
      guides(colour = guide_legend(title = "Areatime")) +
      theme(axis.text.x=element_blank(),
                    panel.background = element_rect(fill = "gray95"),
                    panel.grid.major = element_blank(),
                    panel.grid.minor = element_blank(),
                    axis.ticks.x = element_blank(),
                    axis.line.x = element_blank(),
                    axis.line.y = element_line("black")) +
      facet_grid(. ~Treatment, scales="free", switch = "x") +
      labs(y = "Average Areatime [sec]")
    

    Solution

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2023-03-16
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多