简体   繁体   English

r 中带有 geom_point 的 gg 绘图(ggplot2)

[英]gg plot with geom_point in r (ggplot2)

i would like to do a ggplot with 2 categorical variable in axis Y, trying to get a plot like this:我想在 Y 轴上做一个带有 2 个分类变量的 ggplot,试图得到这样的图: 在此处输入图片说明

but something is wrong in my code:但我的代码有问题:

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")

but the plot that i get have really low values for high and low, and this is not possible.但是我得到的情节对于高和低的值非常低,这是不可能的。 在此处输入图片说明

I cant copy my data because is so long but its basic like :我无法复制我的数据,因为它太长了,但它的基本信息是:

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

To achieve your desired result convert your data to long format.为了达到您想要的结果,将您的数据转换为长格式。 Additionally as you plot two different variables on the y axis I would suggest to make use of faceting which also allows for easy labelling:此外,当您在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()
  )

DATA数据

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM