Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-01 22:03:24 10月02日 Update

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.

Tags: Legend boundary grouping parameter circle ellipse gray background language content function data analysis complexity in this case professional small and medium theme rich in content as above Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Linux Apple Docker Microsoft