我想使用条形图和ggplot
可视化数据。
我有下面的数据,当我要查看此数据时,在Bar下方未显示负值的文本。
dat <- read.table(text = "sample Types Value
sample1 A -36
sample2 B 31
sample1 C 15
sample2 D -12
sample1 E 27
sample2 F 16
sample2 G -10
sample2 H 2
sample2 I 6
sample2 J -7
sample2 K -8"
, header=TRUE)
library(ggplot2)
px <- ggplot(data = dat , aes(x = Types , y = Value , Colour = Types ))
px + geom_bar(stat = "identity" ,color = "#FFFFFF" , fill = "dodgerblue3") +
geom_text(aes(label=Value), position=position_dodge(width=0.9), hjust= -.25,vjust=0.25 ,size =3.5 , angle = 90)
最佳答案
通过geom_text
在y轴上传递y = Value + 2 * sign(Value)
的位置
library(ggplot2)
ggplot(dat, aes(Types, Value)) +
geom_bar(stat = "identity" ,color = "#FFFFFF" , fill = "dodgerblue3") +
geom_text(aes(y = Value + 2 * sign(Value), label = Value),
position = position_dodge(width = 0.9),
size = 3.5 , angle = 90)
我在情节上进行的另一处视觉上有细微调整的情节:
由于您的数字带有条形,因此不需要y轴(这是多余的)。
ggplot(dat, aes(Types, Value)) +
geom_bar(stat = "identity", color = "black" , fill = "grey",
size = 0.7, width = 0.9) +
geom_text(aes(y = Value + 2 * sign(Value), label = Value),
position = position_dodge(width = 0.9),
size = 5) +
theme_classic() +
theme(axis.text.x = element_text(size = 12),
axis.title = element_text(size = 20),
axis.text.y = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank())
关于r - 将标签放在正负geom_bar上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46300666/