1

我需要在我的哑铃图中添加带有 % 符号的数据标签。知道怎么做吗?我有尝试执行此操作的代码,但收到错误“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)))


4

1 回答 1

0

数据文件不再可用,所以这是我从另一个站点获得的一些数据获取一些数据:来自https://plotly.com/r/dumbbell-plots/的示例数据

如果您在这里仍然需要帮助或可以帮助其他人,这可能会有所帮助

谢谢

##Get some data
s <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
s$School <- factor(s$School, levels = s$School[order(s$Men)])
s1<-s %>% mutate("type" = 'Gender earnings disparity') 

##Use the Dumbbell R package to Plot



##Plot
plot1<-dumbbell::dumbbell(xdf=s1,id="School",key = "type", column1 = "Women", column2 = "Men", delt=1 ,arrow = 1, lab1 = "Women", lab2="Men", p_col1 = "red", p_col2 = "blue" ,pt_val = 1 , pval = 2, textsize = 3) + 
  xlim(60,170) + 
  facet_wrap( . ~ type)

哑铃 R 包

于 2021-02-26T18:13:28.323 回答