出城逢虎第五难
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
#按照分组进行排序
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
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
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
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
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
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
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://www.jianshu.com/p/c0cf31afe1a0
作者:Zee_李海海