Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use R language ggplot2 to remove gray background and add ellipse and circle grouping boundary

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, Xiaobian will bring you about how to use R language ggplot2 to remove gray background, add oval and circular grouping boundaries, the article is rich in content and professional analysis and description for everyone, after reading this article I hope you can gain something.

Legend for General Bubble Plot

The sample data is directly from the built-in iris dataset

library(ggplot2)

colnames(iris)

ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+

geom_point(aes(size=Petal.Length,color=Species))+

guides(color=F)+

scale_size_continuous(range = c(5,10),

breaks = c(2,4,6))

image.png

image.png How does that become a hollow circle like this?

I began to think complicated, thinking that I need to go to the legend related parameters to set, originally directly change the shape of the point is good, to the shape parameter set to 21 is good

ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+

geom_point(aes(size=Petal.Length,color=Species),

shape=21)+

guides(color=F)+

scale_size_continuous(range = c(5,10),

breaks = c(2,4,6))

image.png

In this case, the points on the graph also become hollow. If you want to set the points on the graph to solid, you can add a fill parameter.

ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+

geom_point(aes(size=Petal.Length,

color=Species,

fill=Species),

shape=21)+

guides(color=F,fill=F)+

scale_size_continuous(range = c(5,10),

breaks = c(2,4,6))

image.png

Here you can also see that the legend has a gray background. What if you want to remove it? The answer is to set the legend.key parameter in the theme

ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+

geom_point(aes(size=Petal.Length,

color=Species,

fill=Species),

shape=21)+

guides(color=F,fill=F)+

scale_size_continuous(range = c(5,10),

breaks = c(2,4,6))+

theme(legend.key = element_blank())

image.png

What is the Chinese meaning of key here?

image.png Add elliptical grouping boundaries

The stat_ellipse() function is used

ggplot(data=iris,aes(x=Sepal.Length,

y=Sepal.Width,

color=Species))+

geom_point()+

theme(legend.key = element_blank())+

stat_ellipse(aes(x=Sepal.Length,

y=Sepal.Width,

color=Species,

fill=Species),

geom = "polygon",

alpha=0.5)

image.png Add a circular grouping boundary

The geom_circle() function in ggforce is used.

library(ggplot2)

library(ggforce)

colnames(iris)

ggplot()+

geom_point(data=iris,aes(x=Sepal.Length,

y=Sepal.Width,

color=Species))+

theme(legend.key = element_blank(),

panel.background = element_blank(),

panel.border = element_rect(color="black",

fill = "transparent"))+

geom_circle(aes(x0=5,y0=3.5,r=1),

fill="blue",

alpha=0.2,

color="red")+

xlim(2,8)+

ylim(2,8)+

geom_circle(aes(x0=7,y0=3,r=1),

fill="green",

alpha=0.2,

color="red")

The above is how to use R language ggplot2 to remove the gray background, add elliptical and circular grouping boundaries, if there is a similar doubt, may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report