【发布时间】:2018-11-14 10:02:42
【问题描述】:
我有以下从the rgraph gallery 获取的棒棒糖/哑铃 ggplot 的最小可重复示例。
如何使项目符号端点因查找表而异?行数将是动态的,因此不应对其进行硬编码。因此,例如,如果下端为负,则显示左箭头,如果为零,则应显示子弹,如果为正,则应显示右箭头。
我还想根据这些改变每条线/哑铃的颜色。因此,例如,如果下端为负,则整条线为红色,如果它触及零,则整条线为蓝色,严格为正,则应显示为绿色。
library(tidyverse)
# Create data
value1=abs(rnorm(26))*2
data=data.frame(x=LETTERS[1:26], value1=value1, value2=value1+1+rnorm(26, sd=1) )
# Reorder data using average?
data = data %>% rowwise() %>% mutate( mymean = mean(c(value1,value2) )) %>% arrange(mymean) %>% mutate(x=factor(x, x))
# plot
ggplot(data) +
geom_segment( aes(x=x, xend=x, y=value1, yend=value2), color="grey") +
geom_point( aes(x=x, y=value1), color=rgb(0.2,0.7,0.1,0.5), size=3 ) +
geom_point( aes(x=x, y=value2), color=rgb(0.7,0.2,0.1,0.5), size=3 ) +
coord_flip()
# With a bit more style
ggplot(data) +
geom_segment( aes(x=x, xend=x, y=value1, yend=value2), color="grey") +
geom_point( aes(x=x, y=value1), color=rgb(0.2,0.7,0.1,0.5), size=3 ) +
geom_point( aes(x=x, y=value2), color=rgb(0.7,0.2,0.1,0.5), size=3 ) +
coord_flip()+
theme_light() +
theme(
legend.position = "none",
panel.border = element_blank()
) +
xlab("") +
ylab("Value of Y")
【问题讨论】: