我需要在我的哑铃图中添加带有 % 符号的数据标签。知道怎么做吗?我有尝试执行此操作的代码,但收到错误“x * 100 中的错误:二进制运算符的非数字参数”。是否可以增加图表上语句的字体?请参阅下面的代码,以及此处的数据和图表
数据:https ://gofile.io/d/k8vadK 图:https ://imgur.com/a/dkiRuZv
blue <- "#0171CE"
red <- "#DE4433"
percent_first <- function(x) {
x <- sprintf("%d%%", round(x*100))
x[2:length(x)] <- sub("%$", "", x[2:length(x)])
x
}
library(ggplot2)
library(ggalt)
library(tidyverse)
ggplot() +
geom_segment(data=overall, aes(y=statement, yend=statement, x=0.8, xend=1), color="#b2b2b2", size=0.15) +
geom_dumbbell(data=overall, aes(y=statement, x=prior, xend=current),
size=1.5, color="#b2b2b2", size_x=3, size_xend = 3, colour_x = red, colour_xend = blue) +
geom_text(data=filter(overall, statement=="The meeting was well organized"),
aes(x=current, y=statement, label="2020"),
color=blue, size=3, vjust=-1.5, fontface="bold", family="Arial") +
geom_text(data=filter(overall, statement=="The meeting was well organized" ),
aes(x=prior, y=statement, label="2019"),
color=red, size=3, vjust=-1.5, fontface="bold", family="Arial") +
geom_text(data=overall, aes(x=prior, y=statement, label=percent_first(rep)),
color=red, size=2.75, vjust=2.5, family="Arial") +
geom_text(data=overall, color=blue, size=2.75, vjust=2.5, family="Arial",
aes(x=prior, y=statement, label=percent_first(dem)))