Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to draw Bubble Chart in R language

Shulou Source: shulou.com Published: 2022-06-01 21:59:19 10月05日 Update

This article mainly explains "how to draw bubble charts with R language". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to draw bubble charts with R language" together.

The first step is to prepare the data.

Some of the data sets are as follows

image.png

4 columns in total

The first two columns are variables, the third column is correlation coefficients, and the fourth column is significant P values.

The variables in front need to be noted, because only the triangle is drawn, so the time to prepare the data is:

The total variables are 10 x1 's in the first column, followed by 9 x2 's, and then 8 x3 's.

Step 1 Tree reads data df%

mutate(label=case_when(

signi0.001&signi0.01&signi df1

And then there's the plot.

First up is the bubble diagram of the upper triangle.

library(ggplot2)

ggplot(data=df1,aes(x=var_x,y=var_y))+

geom_point(aes(size=value,color=value))+

scale_color_gradient(low = "#80fcfe",high = "#ff80fc",

breaks=seq(-1,1,0.2))+

scale_size_continuous(range = c(5,15))+

guides(size=F)

image.png

In this case, X10 is not in the right position, so set the level of the factor

df1$var_x df2

head(df2)

ggplot(data=df1,aes(x=var_x,y=var_y))+

geom_point(aes(size=value,color=value))+

scale_color_gradient(low = "#80fcfe",high = "#ff80fc",

breaks=seq(-1,1,0.2))+

scale_size_continuous(range = c(5,15))+

guides(size=F)+

theme_bw()+

geom_text(data=df2,aes(x=var_y,y=var_x,

label=paste0(value,label)))

image.png

Finally adjust the legend height

df1%>%

filter(var_x!= var_y) -> df2

head(df2)

ggplot(data=df1,aes(x=var_x,y=var_y))+

geom_point(aes(size=value,color=value))+

scale_color_gradient(low = "#80fcfe",high = "#ff80fc",

breaks=seq(-1,1,0.2))+

scale_size_continuous(range = c(5,15))+

guides(size=F)+

theme_bw()+

geom_text(data=df2,aes(x=var_y,y=var_x,

label=paste0(value,label)))+

theme(legend.key.height = unit(3.5,'cm'),

legend.justification = c(0,0),

legend.title = element_blank())

Thank you for reading, the above is "how to draw bubble diagram with R language" content, after the study of this article, I believe we have a deeper understanding of how to draw bubble diagram with R language, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Bubbles language data next variables learning salience content trigonometry preparation no right in that case position factor legend diagonal diagonal that is ideas. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS MariaDB Shulou Information Huawei