Bar plots (条形图)
library(ggpubr)
library(ggplot2)
data("mtcars")
df<-mtcars
df$cyl<-as.factor(df$cyl)
df$name<-rownames(df)
p<-ggbarplot(df,x="name",y="mpg",fill="cyl",color="white",x.text.angle = 90 ,sort.by.groups = FALSE,sort.val = "desc",palette = "jco")
p
![](https://img.haomeiwen.com/i19696329/eb965a88889ad5c3.png)
#按照分组进行排序
p<-ggbarplot(df,x="name",y="mpg",fill="cyl",color="white",x.text.angle = 90 ,sort.by.groups = T,sort.val = "desc",palette = "jco") #sort.by.groups = T
p
![](https://img.haomeiwen.com/i19696329/92939fbac2ad2039.png)
Calculate the z-score of the mpg data:
df$mpg_z <- (df$mpg -mean(df$mpg))/sd(df$mpg)
df$mpg_grp <- factor(ifelse(df$mpg_z < 0, "low", "high"),levels = c("low", "high"))
p<-ggbarplot(df,x="name",y="mpg_z",fill="mpg_grp",color="white",x.text.angle = 90 ,sort.by.groups = F,sort.val = "asc",palette = "jco",ylab = "MPG z-score",xlab = FALSE,legend.title = "MPG Group")
p
![](https://img.haomeiwen.com/i19696329/1ef4330344e8a360.png)
Rotate the plot: use rotate = TRUE and sort.val = “desc”
p<-ggbarplot(df,x="name",y="mpg_z",fill="mpg_grp",color="white",x.text.angle = 90 ,sort.by.groups = F,sort.val = "desc",palette = "jco",ylab = "MPG z-score",xlab = FALSE,legend.title = "MPG Group",rotate = TRUE,ggtheme = theme_minimal())
p
![](https://img.haomeiwen.com/i19696329/d8eca0868d073709.png)
Dot charts
p1<-ggdotchart(df, x = "name", y = "mpg",
color = "cyl", # Color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
sorting = "ascending", # Sort value in descending order
add = "segments", # Add segments from y = 0 to dots
ggtheme = theme_pubr() # ggplot2 theme
)
p1
![](https://img.haomeiwen.com/i19696329/d79b19327afc532b.png)
Sort in decending order. sorting = “descending”.
Rotate the plot vertically, using rotate = TRUE.
Sort the mpg value inside each group by using group = “cyl”.
Set dot.size to 6.
Add mpg values as label. label = “mpg” or label = round(df$mpg).
p1<-ggdotchart(df, x = "name", y = "mpg",
color = "cyl", # Color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
sorting = "descending", # Sort value in descending order
add = "segments", # Add segments from y = 0 to dots
rotate = TRUE, # Rotate vertically
group = "cyl", # Order by groups
dot.size = 6, # Large dot size
label = round(df$mpg), # Add mpg values as dot labels
font.label = list(color = "white", size = 9,
vjust = 0.5), # Adjust label parameters
ggtheme = theme_pubr() # ggplot2 theme
)
p1
![](https://img.haomeiwen.com/i19696329/319cd5c28f9f67f3.png)
p1<-ggdotchart(df, x = "name", y = "mpg_z",
color = "cyl", # Color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
sorting = "descending", # Sort value in descending order
add = "segments", # Add segments from y = 0 to dots
add.params = list(color = "lightgray", size = 2), # Change segment color and size
group = "cyl", # Order by groups
dot.size = 6, # Large dot size
label = round(df$mpg_z,1), # Add mpg values as dot labels
font.label = list(color = "white", size = 9,
vjust = 0.5), # Adjust label parameters
ggtheme = theme_pubr() # ggplot2 theme
)+
geom_hline(yintercept = 0, linetype = 2, color = "lightgray")
p1
![](https://img.haomeiwen.com/i19696329/b34939ff33380154.png)
Cleveland’s dot plot
Color y text by groups. Use y.text.col = TRUE.
p1<-ggdotchart(df, x = "name", y = "mpg",
color = "cyl", # Color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
sorting = "descending", # Sort value in descending order
rotate = TRUE, # Rotate vertically
dot.size = 2, # Large dot size
y.text.col = TRUE, # Color y text by groups
ggtheme = theme_pubr() # ggplot2 theme
)+
theme_cleveland() # Add dashed grids
p1
![](https://img.haomeiwen.com/i19696329/35ed424b83091a10.png)
完结撒花
![](https://img.haomeiwen.com/i19696329/d5b243cc933fd7ab.jpg)
网友评论