【问题标题】:Overlaying points and controlling size with ggplot2用ggplot2覆盖点和控制大小
【发布时间】:2017-04-20 11:25:22
【问题描述】:

我想绘制一些带有几个区间估计的点估计,然后使用不同的颜色和大小叠加真实的点值,并带有颜色图例。

我尝试了很多东西。如果我只是使用对 geom_point 的新调用,我无法弄清楚如何添加图例。因此,我目前的方法是将数据堆叠在自身之上,这很笨拙。即便如此,该图还是出现错误,True 值的蓝色大点,顶部有所需的橙色点。

如果能得到任何帮助,我将不胜感激。

nms <- c("2.5%","25%","50%","75%","97.5%","dose","truep")
a <- c(9.00614679684893e-    44,0.000123271800672435,0.0339603711049475,0.187721170170911,0.67452033450121,5,0.040752445325937)
b <- c(1.59502878028266e-25,0.00328588588499889,0.0738203422543555,0.25210200886225,0.714843425007051,10,0.0885844107052267)
cc <- c(1.41975723605948e-14,0.0184599181547097,0.118284929584256,0.311068595276067,0.74339745948793,15,0.141941915501108)
d <- c(0.0311851190805834,0.154722028150561,0.299318020818234,0.50887634580605,0.838779816278485,25,0.359181624981881)
e <- c(0.0529617924263383,0.289588386297245,0.566777817134668,0.883959271416755,0.999999999999317,40,0.680133380561602)
f <- c(0.0598904847882839,0.327655201251564,0.640100529843672,0.950060245074853,1,50,0.768120635812406)
g <- c(0.0641613025760661,0.355626055560067,0.686504841650593,0.978023943968809,1,60,0.823805809980712)

p <- as.data.frame(t(data.frame(a, b, cc, d, e, f, g)))
names(p) <- nms

# Faff duplicating data
p$truep <- 1.2 * p$truep
p2 <- p
p2[, 1:5] <- p$truep # truep is known, so there are no intervals

p3 <- rbind(p2, p)
p3$wh <- rep((c(2, 3)), each=nrow(p))
p3$col <- rep(c("orange", "blue"), each=nrow(p))

ggplot(p3, aes(dose, `50%`)) +
  geom_point(aes(size=wh, color=col)) +
  scale_size(range=c(5, 7), guide="none") +
  scale_color_manual(name="", labels=c("Prior", "True"), values=c("blue", "orange")) +
  geom_pointrange(aes(ymin=`2.5%`, ymax=`97.5%`, x=dose), color="blue") +
  geom_pointrange(aes(ymin=`25%`, ymax=`75%`, x=dose), color="blue", size=2) +
  geom_point(aes(dose, truep), color="orange") +
  theme(axis.text.x=element_text(size=12), axis.title.x=element_text(size=14),
    axis.text.y=element_text(size=12), axis.title.y=element_text(size=14),
    legend.text=element_text(size=12))

R 3.3.1,ggplot2_2.1.1

谢谢, 哈利

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我通过将数据集分成两部分找到了解决方案:

    library(dplyr)
    
    priors <- p%>%
      mutate(datatype = 'Prior')
    
    truevals <-  p%>%
      select(dose, truep)%>%
      mutate(datatype = 'True')
    
    
    ggplot(truevals, aes(x = dose, y = truep, colour = datatype))+
      geom_pointrange(data = priors, aes(ymin=`25%`, ymax=`75%`, y = `50%`), size=1.5) +
      geom_pointrange(data = priors, aes(ymin=`2.5%`, ymax=`97.5%`, y = `50%`))+
      geom_point()+
      scale_color_manual(name="", values=c("Prior" = "blue", "True" = "orange")) +
      theme(axis.text.x=element_text(size=12), axis.title.x=element_text(size=14),
            axis.text.y=element_text(size=12), axis.title.y=element_text(size=14),
            legend.text=element_text(size=12))
    

    首先,我们根据具有先验的数据集绘制两个点范围。然后是实际值。通过向两个数据集添加具有数据类型的行,我们可以添加图例。结果如下图:

    【讨论】:

    • 谢谢!我现在有了我想要的情节。
    【解决方案2】:

    对于ggplot2::geom_point() 方法,有一个show.legend 属性,默认为NA,因此将其设置为TRUE 应该会有所帮助。

    您可以使用标签属性添加图例,如下所示:

    ggplot2::scale_fill_manual(values = c("red", "black",
                               labels = c("Number of people",
                                          "Number of birds"))
    

    您已经在使用labels=c("Prior", "True") 进行此操作

    您还可以通过以下方式更改图例的外观:

    ggplot2::theme(legend.position = "bottom",
                   legend.text = ggplot2::element_text(size = 22),
                   legend.box = "horizontal",
                   legend.key = ggplot2::element_blank())
    

    【讨论】:

    • 我与 scale_fill_manual 和 scale_color_manual 战斗了好久,但无法让图例出现。 Jeroen Boeye 的解决方案有效,但将数据分成 2 份。谢谢您的建议。
    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多