美文网首页R plotR语言做图
ggpubr包学习(三)

ggpubr包学习(三)

作者: Zee_李海海 | 来源:发表于2021-03-03 17:11 被阅读0次

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

完结撒花

完结撒花.jpg

相关文章

网友评论

    本文标题:ggpubr包学习(三)

    本文链接:https://www.haomeiwen.com/subject/qwgqqltx.html