如何向 ggplot2 条形图中的标签添加水平闪避?
How to add horizontal dodge to labels in a ggplot2 bar chart?
我正在尝试将标签添加到一个相当简单的条形图(即 geom_bar)。 geom_text()
内的 position_dodge()
更正标签的垂直间距,但不是水平间距。我怎样才能 ggplot2
将我的标签正确地分散在条形图上方?
library(tidyr)
library(grid)
library(ggplot2)
data = read.table('temp.dat', header=T)
data <- gather(data, SOA, RT, X0:X1000)
data$ResponseCondition = as.factor(data$ResponseCondition)
levels(data$SOA) = c(0,250,500,1000)
data$SOA = as.numeric(as.character(data$SOA))
p = ggplot(data, aes(y=RT, x=SOA, fill=ResponseCondition, ymax=RT*1.05))
p = p + geom_bar(stat='identity', position=position_dodge())
p = p + geom_text(aes(label=RT), position=position_dodge())
p = p + scale_x_continuous(breaks=c(0,250,500,1000))
p = p + ylab('Response Time (ms)')
p = p + xlab('Precue Interval (ms)')
p = p + theme_bw()
p = p + scale_fill_grey(start = 0.1, end = .9, name='Response condition')
p = p + theme(
axis.title.x = element_text(vjust=-0.30, size=10),
axis.title.y = element_text(vjust=1.50, size=10),
text = element_text(size=10),
legend.justification=c(1,1), legend.position=c(1,1),
plot.margin = unit(rep(.5, 4), 'cm'))
ggsave('temp.png', width=10, height=7.5)
这是条形图:
这里是 temp.dat
的内容,需要使其成为一个完整的示例:
ResponseCondition 0 250 500 1000
28 1254 1056 901 864
46 1306 1063 889 772
64 1171 939 786 682
82 1205 948 821 731
我认为,它们没有被正确分散的主要原因是 SOA 不是一个因子变量,你应该保留它一个因子。此外,如果您跳过 scale_x_continuous,您的代码应该可以工作。这就是我做的一些小改动。您可以按照自己喜欢的方式调整垂直距离 (vjust) 和颜色:
library(tidyr)
library(grid)
library(ggplot2)
data = read.table('temp.dat', header=T)
data <- gather(data, SOA, RT, X0:X1000)
data$ResponseCondition = as.factor(data$ResponseCondition)
levels(data$SOA) = c(0,250,500,1000)
# data$SOA = as.numeric(as.character(data$SOA))
p = ggplot(data, aes(y=RT, x=SOA, fill=ResponseCondition, ymax=RT*1.05))
p = p + geom_bar(stat='identity', position=position_dodge())
p = p + geom_text(aes(label=RT), colour="purple", vjust=1.5, position=position_dodge(0.9), size=4)
# p = p + scale_x_continuous(breaks=c(0,250,500,1000))
p = p + ylab('Response Time (ms)')
p = p + xlab('Precue Interval (ms)')
p = p + theme_bw()
p = p + scale_fill_grey(start = 0.1, end = .9, name='Response condition')
p = p + theme(
axis.title.x = element_text(vjust=-0.30, size=10),
axis.title.y = element_text(vjust=1.50, size=10),
text = element_text(size=10),
legend.justification=c(1,1), legend.position=c(1,1),
plot.margin = unit(rep(.5, 4), 'cm'))
ggsave('temp.png', width=10, height=7.5)
我正在尝试将标签添加到一个相当简单的条形图(即 geom_bar)。 geom_text()
内的 position_dodge()
更正标签的垂直间距,但不是水平间距。我怎样才能 ggplot2
将我的标签正确地分散在条形图上方?
library(tidyr)
library(grid)
library(ggplot2)
data = read.table('temp.dat', header=T)
data <- gather(data, SOA, RT, X0:X1000)
data$ResponseCondition = as.factor(data$ResponseCondition)
levels(data$SOA) = c(0,250,500,1000)
data$SOA = as.numeric(as.character(data$SOA))
p = ggplot(data, aes(y=RT, x=SOA, fill=ResponseCondition, ymax=RT*1.05))
p = p + geom_bar(stat='identity', position=position_dodge())
p = p + geom_text(aes(label=RT), position=position_dodge())
p = p + scale_x_continuous(breaks=c(0,250,500,1000))
p = p + ylab('Response Time (ms)')
p = p + xlab('Precue Interval (ms)')
p = p + theme_bw()
p = p + scale_fill_grey(start = 0.1, end = .9, name='Response condition')
p = p + theme(
axis.title.x = element_text(vjust=-0.30, size=10),
axis.title.y = element_text(vjust=1.50, size=10),
text = element_text(size=10),
legend.justification=c(1,1), legend.position=c(1,1),
plot.margin = unit(rep(.5, 4), 'cm'))
ggsave('temp.png', width=10, height=7.5)
这是条形图:
这里是 temp.dat
的内容,需要使其成为一个完整的示例:
ResponseCondition 0 250 500 1000
28 1254 1056 901 864
46 1306 1063 889 772
64 1171 939 786 682
82 1205 948 821 731
我认为,它们没有被正确分散的主要原因是 SOA 不是一个因子变量,你应该保留它一个因子。此外,如果您跳过 scale_x_continuous,您的代码应该可以工作。这就是我做的一些小改动。您可以按照自己喜欢的方式调整垂直距离 (vjust) 和颜色:
library(tidyr)
library(grid)
library(ggplot2)
data = read.table('temp.dat', header=T)
data <- gather(data, SOA, RT, X0:X1000)
data$ResponseCondition = as.factor(data$ResponseCondition)
levels(data$SOA) = c(0,250,500,1000)
# data$SOA = as.numeric(as.character(data$SOA))
p = ggplot(data, aes(y=RT, x=SOA, fill=ResponseCondition, ymax=RT*1.05))
p = p + geom_bar(stat='identity', position=position_dodge())
p = p + geom_text(aes(label=RT), colour="purple", vjust=1.5, position=position_dodge(0.9), size=4)
# p = p + scale_x_continuous(breaks=c(0,250,500,1000))
p = p + ylab('Response Time (ms)')
p = p + xlab('Precue Interval (ms)')
p = p + theme_bw()
p = p + scale_fill_grey(start = 0.1, end = .9, name='Response condition')
p = p + theme(
axis.title.x = element_text(vjust=-0.30, size=10),
axis.title.y = element_text(vjust=1.50, size=10),
text = element_text(size=10),
legend.justification=c(1,1), legend.position=c(1,1),
plot.margin = unit(rep(.5, 4), 'cm'))
ggsave('temp.png', width=10, height=7.5)