數(shù)據(jù)下載來(lái)源于:
https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/10_OneNumSevCatSubgroupsSevObs.csv
參考代碼來(lái)自于:Violin plot with ggstatsplot – the R Graph Gallery (r-graph-gallery.com)
大家可以一起共同學(xué)習(xí)
此次用的包是ggstatsplot
install.packages(c("ggstatsplot","palmerpenguins","tidyverse"))
library(ggstatsplot)
library(palmerpenguins)
library(tidyverse)
library(rstantools)
#上傳數(shù)據(jù)
getwd()
y <- read.table("D:/biolearning/Rcourse/happy.txt", header=T, sep=",")
x <- data.frame(y)#轉(zhuǎn)換數(shù)據(jù)類型
class(x)#確認(rèn)數(shù)據(jù)類型是否轉(zhuǎn)換成功
#先刪除缺失值
x <- drop_na(x)
x
view(x)#查看輸入的數(shù)據(jù)類型
colnames(x)#查看列名
#基礎(chǔ)繪圖
plt <- ggbetweenstats(
data = x,
x = sex,
y = total_bill
)
plt
基礎(chǔ)圖繪制結(jié)果
Rplot01.png
#添加標(biāo)題和標(biāo)簽
plt <- plt +
# Add labels and title
labs(
x = "sex",
y = "total_bill",
title = "the money"#加標(biāo)題
) +
# Customizations
theme(
# This is the new default font in the plot
text = element_text(family = "Roboto", size = 8, color = "black"),
plot.title = element_text(
family = "Lobster Two",
size = 20,
face = "bold",
color = "#2a475e"
),
# Statistical annotations below the main title
plot.subtitle = element_text(
family = "Roboto",
size = 15,
face = "bold",
color="#1b2838"
),
plot.title.position = "plot", # slightly different from default
axis.text = element_text(size = 10, color = "black"),
axis.title = element_text(size = 12)
)
plt
添加標(biāo)簽后的圖
Rplot02-lab.png
# 1. 刪除軸刻度
# 2. 用較淺的顏色更改軸線的默認(rèn)顏色
# 3. 刪除大部分參考線溢吻,只保留主要的水平線
# 4. 將面板和背景填充設(shè)置為相同的淺色
plt <- plt +
theme(
axis.ticks = element_blank(),
axis.line = element_line(colour = "grey50"),
panel.grid = element_line(color = "#b4aea9"),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(linetype = "dashed"),
panel.background = element_rect(fill = "#fbf9f4", color = "#fbf9f4"),
plot.background = element_rect(fill = "#fbf9f4", color = "#fbf9f4")
)
保存照片
#使用ggsave儲(chǔ)存照片格式為png
ggsave("happy.png", plot = plt,width = 8,height = 8)
image.png
happy.png