Bubble chart with R

痞子三分冷 提交于 2021-02-11 13:54:15

问题


I have a table with an abundance of species in different months. I want to make a bubble chart wherein the y-axis for abundance species and x-axis for the month while the radius of the bubbles for the abundance of species that I found in the month.

My data arrangement is like this

enter image description here

I want to end up the bubble chart with like this

le

Please help me. Thank you


回答1:


The following is a bubble graph created with a mock data set. I have tried to show how to plot a data set with a structure like the data set in the question and, as usual, this sort of problem is a data reformating problem. See reshaping data.frame from wide to long format. In this case I had to first have the row names as a new column. Only then the data was reformated.

library(tidyverse)
library(ggplot2)

df1 %>%
  rownames_to_column(var = "id") %>%
  gather(key, Abundance, -id) %>%
  ggplot(aes(key, id)) +
  geom_point(aes(size = Abundance), colour = "red", fill = "red", shape = 21) +
  labs(x = "", y = "") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Data creation code.

set.seed(1234)
df1 <- matrix(0, nrow = 10, ncol = 5)
df1[sample(prod(dim(df1)), 10)] <- sample(200:250, 10)
df1 <- as.data.frame(df1)
dimnames(df1) <- list(letters[1:10], LETTERS[1:5])


来源:https://stackoverflow.com/questions/60025301/bubble-chart-with-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!