r语⾔barplot函数图中加标签_ggplot2条形图添加数据标签title: "添加数据标签"
author: "wintryheart"
date: "2019/7/21"
output: html_document
knitr::opts_chunk$set(echo = TRUE)
数据
library(reshape2)
y2000position标签属性
y2010
nesttype
emptynest
knitr::kable(emptynest)
emptynest2
with(emptynest2,
nesttype
year
)
简单的条形图
library(ggplot2)
library(gridExtra)
p1
p2
grid.arrange(p1, p2, ncol=1)
geom_col()和geom_bar()的区别在于,geom_col()默认使⽤原数据,⽽geom_bar()默认进⾏count。
12.png
group分组问题
在上图上,ggplot()并没有设置aes()的group参数。这种做法不影响geom_bar(),但是影响geom_text(),使得数据标签不能正确显⽰。
p3
geom_col(aes(fill=year), position="dodge") +
geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0)
p4
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()的参数设置问题
position问题
对于多组数据,不能直接设置
,⽽要⽤position_dodge()设置不同组之间标签的避开宽度(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问题
vjust选项调节数据标签相对于条形顶部的位置。默认为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()问题
⽬的是抬升标签的位置,使得标签脱离条形的顶部。默认是贴合。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
附录
下⾯内容节选⾃geom_text()帮助⽂件。
# Aligning labels and bars --------------------------------------------------
df
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
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论