Условная форма / Цветовой график - PullRequest
0 голосов
/ 07 декабря 2018

У меня есть фрейм данных, который выглядит следующим образом

categories <- c('a', 'b', 'c', 'd', 'e')
trends <- c(.20, -.05, 0, .1, 0)

df <- as.data.frame(categories, trends)

Я пытаюсь создать график, который имеет зеленые треугольники для положительных трендов, красные треугольники для отрицательных трендов и черные квадраты для нулевых трендов.

Это моя попытка, но цвета и руководство не получаются правильными.

ggplot(
  df %>%
    mutate(color_index = ifelse(trends > 0, "green", 
                         ifelse(trends < 0, "red", "black")),
           shape_id = ifelse(trends > 0, 24, 
                      ifelse(trends < 0, 25, 22))),

  aes(x = categories, y = trends, fill = color_index)) +

  # up/down arrow points
  geom_point(aes(shape = shape_id), size = 7) +
  scale_shape_identity() +
  geom_text(aes(label=trends*100), size = 4, nudge_y=-0.01, check_overlap = TRUE) 

enter image description here

1 Ответ

0 голосов
/ 07 декабря 2018

Добавьте scale_fill_identity на свой участок

ggplot(df1, aes(x = categories, y = trends, fill = color_index)) +
  # up/down arrow points
  geom_point(aes(shape = shape_id), size = 7) +
  scale_shape_identity() +
  scale_fill_identity() +
  geom_text(aes(label=trends*100), size = 4, nudge_y=-0.01, check_overlap = TRUE) 

enter image description here

данные

categories <- c('a', 'b', 'c', 'd', 'e')
trends <- c(.20, -.05, 0, .1, 0)

df <- data.frame(categories, trends)
df1 <- df %>%
  mutate(color_index = ifelse(trends > 0, "green", 
                              ifelse(trends < 0, "red", "black")),
         shape_id = ifelse(trends > 0, 24, 
                           ifelse(trends < 0, 25, 22)))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...