简体   繁体   中英

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: 在此处输入图片说明

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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