title: "添加數(shù)據(jù)標(biāo)簽"
author: "wintryheart"
date: "2019/7/21"
output: html_document
knitr::opts_chunk$set(echo = TRUE)
數(shù)據(jù)
library(reshape2)
y2000 <- c(77.17, 11.46, 11.38)
y2010 <- c(68.23, 16.4,15.37)
nesttype <- c("與子女生活","獨(dú)居空巢","夫妻空巢")
emptynest <- data.frame(nesttype,y2000, y2010)
knitr::kable(emptynest)
emptynest2 <- melt(emptynest, id.vars = "nesttype", variable.name = "year", value.name = "proportion")
with(emptynest2,
nesttype <- factor(nesttype, levels = c("與子女生活","夫婦空巢","獨(dú)居空巢"), labels = c("與子女生活","夫婦空巢","獨(dú)居空巢"), ordered=T),
year <- substr(year, 2, 5)
)
簡(jiǎn)單的條形圖
library(ggplot2)
library(gridExtra)
p1 <- ggplot(emptynest2, aes(x=nesttype, y=proportion)) + geom_bar(aes(fill=year), position="dodge", stat="identity")
p2 <- ggplot(emptynest2, aes(x=nesttype, y=proportion)) + geom_col(aes(fill=year), position="dodge")
grid.arrange(p1, p2, ncol=1)
- geom_col()和geom_bar()的區(qū)別在于塌衰,geom_col()默認(rèn)使用原數(shù)據(jù)诉稍,而geom_bar()默認(rèn)進(jìn)行count。
12.png
group分組問(wèn)題
在上圖上猾蒂,ggplot()并沒(méi)有設(shè)置aes()的group參數(shù)均唉。這種做法不影響geom_bar(),但是影響geom_text()肚菠,使得數(shù)據(jù)標(biāo)簽不能正確顯示舔箭。
p3 <- ggplot(emptynest2, aes(x=nesttype, y=proportion)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0)
p4 <- ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0)
grid.arrange(p3,p4,ncol=1)
13.png
geom_text()的參數(shù)設(shè)置問(wèn)題
position問(wèn)題
對(duì)于多組數(shù)據(jù),不能直接設(shè)置蚊逢,而要用position_dodge()設(shè)置不同組之間標(biāo)簽的避開(kāi)寬度(dodging width)层扶。
ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+0.05), position="dodge", vjust=0)
ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0)
21.png
22.png
vjust問(wèn)題
vjust選項(xiàng)調(diào)節(jié)數(shù)據(jù)標(biāo)簽相對(duì)于條形頂部的位置。默認(rèn)為0.5烙荷。
ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9))
ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0)
ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=1)
31.png
32.png
33.png
aes()問(wèn)題
目的是抬升標(biāo)簽的位置镜会,使得標(biāo)簽脫離條形的頂部。默認(rèn)是貼合终抽。
ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion), position=position_dodge(0.9), vjust=0)
ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) +
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+1), position=position_dodge(0.9), vjust=0)
41.png
42.png
附錄
下面內(nèi)容節(jié)選自geom_text()幫助文件戳表。
# Aligning labels and bars --------------------------------------------------
df <- data.frame(
x = factor(c(1, 1, 2, 2)),
y = c(1, 3, 2, 1),
grp = c("a", "b", "a", "b")
)
# ggplot2 doesn't know you want to give the labels the same virtual width
# as the bars:
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp), position = "dodge") +
geom_text(aes(label = y), position = "dodge")
# So tell it:
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp), position = "dodge") +
geom_text(aes(label = y), position = position_dodge(0.9))
# Use you can't nudge and dodge text, so instead adjust the y position
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp), position = "dodge") +
geom_text(
aes(label = y, y = y + 0.05),
position = position_dodge(0.9),
vjust = 0
)
# Justification -------------------------------------------------------------
df <- data.frame(
x = c(1, 1, 2, 2, 1.5),
y = c(1, 2, 1, 2, 1.5),
text = c("bottom-left", "bottom-right", "top-left", "top-right", "center")
)
ggplot(df, aes(x, y)) +
geom_text(aes(label = text))
ggplot(df, aes(x, y)) +
geom_text(aes(label = text), vjust = "inward", hjust = "inward")
51.png
52.png
53.png
61.png
62.png