之前的推文介紹了一個鏈接巷懈,https://simplystatistics.org/2019/08/28/you-can-replicate-almost-any-plot-with-ggplot2/
這個里面有5個R語言ggplot2作圖的實例届腐,數(shù)據(jù)代碼都有仇冯,非常好的學(xué)習(xí)素材。但是他的代碼相對比較長,初學(xué)者看起來可能有些吃力。后面爭取出推文把代碼都拆解一下。今天的推文介紹一下其中的柱形圖實現(xiàn)的代碼
先介紹一個小知識點
ggplot2作圖X軸默認(rèn)坐標(biāo)軸的刻度是朝下的辟宗,Y軸默認(rèn)的刻度是朝左的,如果要改為朝上和朝右吝秕,該如何設(shè)置泊脐。之前也有人問過這個問題
library(ggplot2)
library(ggstar)
ggplot()+
geom_star(aes(x=1,y=1),
size=100,
starshape=16,
fill="red")+
theme_bw()+
theme(axis.ticks.length.x = unit(-1,'cm'),
plot.margin = unit(c(1,1,2,1),'cm'),
axis.text.x = element_text(vjust=-20),
axis.title.x = element_text(vjust=-20),
axis.ticks.length.y = unit(-1,'cm'),
axis.text.y =
element_text(
margin = margin(0,1.2,0,0,'cm')
))
這里我們把axis.ticks.length.x = unit(-1,'cm')
刻度線的長度調(diào)整為負(fù)數(shù)就可以了,
但是還遇到一個問題是 橫坐標(biāo)的文本和標(biāo)題可以通過vjust
參數(shù)上下調(diào)節(jié)烁峭,縱坐標(biāo)的參數(shù)卻不能夠用hjust
的參數(shù)左右調(diào)節(jié)容客,不知道是什么原因
下面開始重復(fù)開頭提到的柱形圖
首先是數(shù)據(jù),用到的是dslabs
這個R包
安裝直接使用命令install.packages("dslabs")
加載數(shù)據(jù)集
library(dslabs)
data("nyc_regents_scores")
給數(shù)據(jù)集增加一列
library(dplyr)
nyc_regents_scores %>% head()
nyc_regents_scores$total <- rowSums(nyc_regents_scores[,-1], na.rm=TRUE)
對數(shù)據(jù)集過濾
如果score這一列是缺失值就把這行數(shù)據(jù)刪除
nyc_regents_scores %>%
filter(!is.na(score)) -> new_df
最基本的柱形圖
new_df %>%
ggplot(aes(score, total)) +
geom_bar(stat = "identity",
color = "black",
fill = "#C4843C")
指定位置添加背景
new_df %>%
ggplot(aes(score, total)) +
annotate("rect", xmin = 65,
xmax = 99,
ymin = 0,
ymax = 35000,
alpha = .5) +
geom_bar(stat = "identity",
color = "black",
fill = "#C4843C")
添加文本注釋
new_df %>%
ggplot(aes(score, total)) +
annotate("rect", xmin = 65,
xmax = 99,
ymin = 0,
ymax = 35000,
alpha = .5) +
geom_bar(stat = "identity",
color = "black",
fill = "#C4843C") +
annotate("text",
x = 66,
y = 28000,
label = "MINIMUM\nREGENTS DIPLOMA\nSCORE IS 65",
hjust = 0,
size = 3) +
annotate("text",
x = 0,
y = 12000,
label = "2010 Regents scores on\nthe five most common tests",
hjust = 0,
size = 3)
修改坐標(biāo)軸刻度和位置
new_df %>%
ggplot(aes(score, total)) +
annotate("rect", xmin = 65,
xmax = 99,
ymin = 0,
ymax = 35000,
alpha = .5) +
geom_bar(stat = "identity",
color = "black",
fill = "#C4843C") +
annotate("text",
x = 66,
y = 28000,
label = "MINIMUM\nREGENTS DIPLOMA\nSCORE IS 65",
hjust = 0,
size = 3) +
annotate("text",
x = 0,
y = 12000,
label = "2010 Regents scores on\nthe five most common tests",
hjust = 0,
size = 3)+
scale_x_continuous(breaks = seq(5, 95, 5),
limit = c(0,99)) +
scale_y_continuous(position = "right") +
ggtitle("Scraping By") +
xlab("") + ylab("Number of tests")
最后是對主題進(jìn)行設(shè)置
new_df %>%
ggplot(aes(score, total)) +
annotate("rect", xmin = 65,
xmax = 99,
ymin = 0,
ymax = 35000,
alpha = .5) +
geom_bar(stat = "identity",
color = "black",
fill = "#C4843C") +
annotate("text",
x = 66,
y = 28000,
label = "MINIMUM\nREGENTS DIPLOMA\nSCORE IS 65",
hjust = 0,
size = 3) +
annotate("text",
x = 0,
y = 12000,
label = "2010 Regents scores on\nthe five most common tests",
hjust = 0,
size = 3)+
scale_x_continuous(breaks = seq(5, 95, 5),
limit = c(0,99)) +
scale_y_continuous(position = "right") +
ggtitle("Scraping By") +
xlab("") +
ylab("Number of tests")+
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.ticks.length = unit(-0.2, "cm"),
plot.title = element_text(face = "bold"))
歡迎大家關(guān)注我的公眾號
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號 主要分享:1约郁、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子缩挑;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)鬓梅、基因組學(xué)供置、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3绽快、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記芥丧!