【问题标题】:gg plot with geom_point in r (ggplot2)r中带有geom_point的ggplot(ggplot2)
【发布时间】:2021-07-17 18:07:43
【问题描述】:

我想在 Y 轴上用 2 个分类变量做一个 ggplot,试图得到一个像这样的图:

但我的代码有问题:

p1<-ggplot(expanded, aes(x= Class,  y= Implementation)) + geom_point(aes(y= Method_reliability)) + geom_point(aes(size= cost_bil))+   rotate_x_text(45) + xlab("Taxonomic Class") + ylab("Method reliability         Implementation") + scale_size_continuous(range = c(1, 20))  + labs(size = "US$ billions", color = "US$ billions")

但我得到的情节的高低值非常低,这是不可能的。

我无法复制我的数据,因为它太长了,但它是基本的:

ID    Implementation   Method_reliability   cost_bil    Class
1     Observed           High                4.5        Amphibia
2     Potential          High                 7         Amphibia
3     Observed           Low                  1.1       Reptilia

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    为了达到您想要的结果,请将您的数据转换为长格式。此外,当您在 y 轴上绘制两个不同的变量时,我建议您使用分面,这也便于标记:

    library(ggplot2)
    library(tidyr)
    
    expanded_long <- expanded %>%
      pivot_longer(c(Implementation, Method_reliability))
    
    ggplot(expanded_long, aes(x = Class, y = value)) +
      geom_point(aes(size = cost_bil)) +
      facet_grid(name ~ ., switch = "y", scales = "free_y") +
      scale_size_continuous(range = c(1, 20)) +
      labs(x = "Taxonomic Class", y = NULL, size = "US$ billions", color = "US$ billions") +
      theme(
        panel.spacing.y = unit(0, "pt"),
        strip.placement = "outside",
        strip.background.y = element_blank()
      )
    

    数据

    structure(list(ID = 1:3, Implementation = c("Observed", "Potential", 
    "Observed"), Method_reliability = c("High", "High", "Low"), cost_bil = c(4.5, 
    7, 1.1), Class = c("Amphibia", "Amphibia", "Reptilia")), class = "data.frame", row.names = c(NA, 
    -3L))
    

    【讨论】:

    • 非常感谢您的帮助,它工作得很好,因为我会添加一个色带,但我有点迷茫
    猜你喜欢
    • 2022-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多