棒棒糖图表的ggplot自定义端点

时间:2018-11-14 10:02:42

标签: r ggplot2

以下是我从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")

1 个答案:

答案 0 :(得分:0)

这是一种方法,但是您必须手动构建图例(如果有)。我担心您要设置颜色的方式掩盖了value1value2中哪一个在左侧(IMO应始终将颜色与度量名称相关联,但您是其中之一)谁想要这样做?它对左项目符号使用纯文本形状,因为您必须做一些有趣的“ arrow()”-或使用使用表情符号或图像的自定义几何图形,否则使用 (其他人也知道如何这样做,实际上有空闲时间肯定应该与那些人回答,因为考虑到这些geom的脆弱性,OP可能会有很多来回的回答,而我只是没有

library(hrbrthemes)
library(tidyverse)
# Create data
set.seed(2018-11-14)

data_frame(
  cond = LETTERS[1:26], 
  value1 = abs(rnorm(26)) * 2,
  value2 = value1 + 1 + rnorm(26, sd = 1)
) -> xdf

# ensure there are representative conditions to test logic
xdf[9, "value1"] <- 0
xdf[10, "value1"] <- 1.1
xdf[11, "value2"] <- 0
xdf[21, "value2"] <- -xdf[21, "value2"]
xdf[26, "value1"] <- -xdf[26, "value1"]
xdf[26, "value2"] <- 0

print(xdf, n=26)

# Reorder data using average?
rowwise(xdf) %>% 
  mutate(mymean = mean(c(value1, value2))) %>% 
  arrange(mymean) %>% 
  mutate(x = factor(cond, cond)) %>% 
  mutate(left_shp = case_when(
    (value1 < 0) | (value2 < 0) ~ "<",
    (value1 == 0) | (value2 == 0) ~ "•",
    (value1 > 0) & (value2 > 0) ~ ">"
  )) %>% 
  mutate(left_size = case_when(
    (value1 < 0) | (value2 < 0) ~ 6,
    (value1 == 0) | (value2 == 0) ~ 11,
    (value1 > 0) & (value2 > 0) ~ 6
  )) %>% 
  mutate(hjust = case_when(
    (value1 < 0) | (value2 < 0) ~ 0.2,
    (value1 == 0) | (value2 == 0) ~ 0.5,
    (value1 > 0) & (value2 > 0) ~ 0.5
  )) %>% 
  mutate(
    col = case_when(
      (value1 < 0) | (value2 < 0) ~ "#cb181d",
      (value1 == 0) | (value2 == 0) ~ "#2171b5",
      (value1 > 0) & (value2 > 0) ~ "#238b45"
    )
  ) %>% 
  mutate(left = ifelse(value1 < value2, value1, value2)) %>% 
  mutate(right = ifelse(value1 > value2, value1, value2)) -> xdf

# plot
ggplot(xdf) +
  geom_segment(
    aes(x = left, xend = right, y = cond, yend = cond, color = I(col)), size = 1
  ) +
  geom_text(
    aes(x = left, y = cond, color = col, label = left_shp, hjust = I(hjust), size = I(left_size))
  ) +
  geom_point(aes(x = right, y = cond, color = col), size = 3) +
  labs(x=NULL, y= NULL) +
  theme_ipsum_rc(grid="X")

enter image description here