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
Barplot.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
Barplot01.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
Barplot02.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
Barplot03.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
Dotplot04.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
dotplot05.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
dotplot07.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
dotplot08.png
网友评论