ggplot2: цветовая зона между двумя перекладинами - PullRequest
0 голосов
/ 24 ноября 2018

Можно ли закрасить только часть диаграммы в ggplot2?
Мне бы хотелось, чтобы цветная область между перекладинами: "Среднесуточный максимум" и "Среднесуточный минимум".Это мои данные:

temperature <- read.table(text = 'X  1  2  3  4  5  6
                          1 "Highest temperature" 31 40 39 34 39 42
                          2 "Mean daily maximum" 27 34 32 30 34 35
                          3 "Mean daily minimum" 26 31 31 30 31 35
                          4 "Lowest temperature" 18 20 20 20 20 24', header =T)

t <- dcast(melt(temperature), variable~X)
ggplot(t, aes(x=variable,ymin = `Lowest temperature`, 
              ymax = `Highest temperature`, lower = `Lowest temperature`, 
              upper = `Highest temperature`, middle = `Mean daily maximum`)) + 
  geom_boxplot(stat = 'identity') +
  xlab('Number') + 
  ylab('Temperature') +
  geom_crossbar(aes(y = `Mean daily maximum` ))+
  geom_crossbar(aes(y = `Mean daily minimum`))

1 Ответ

0 голосов
/ 24 ноября 2018
library(ggplot2)
library(reshape2)

read.table(
  text = 'X  1  2  3  4  5  6
1 "Highest temperature" 31 40 39 34 39 42
2 "Mean daily maximum" 27 34 32 30 34 35
3 "Mean daily minimum" 26 31 31 30 31 35
4 "Lowest temperature" 18 20 20 20 20 24', 
  header = TRUE
) -> temperature

temp_long <- reshape2::dcast(reshape2::melt(temperature), variable~X)

ggplot(
  temp_long, 
  aes(
    x = variable,
    ymin = `Lowest temperature`, 
    ymax = `Highest temperature`, 
    lower = `Lowest temperature`, 
    upper = `Highest temperature`, 
    middle = `Mean daily maximum`
  )
) + 
  geom_boxplot(stat = 'identity') +
  geom_rect(
    aes(
      xmin = as.numeric(variable)+0.45, 
      xmax = as.numeric(variable)-0.45,
      ymin = `Mean daily minimum`,
      ymax = `Mean daily maximum`
    ),
    fill = "#a50f15"
  ) +
  geom_crossbar(aes(y = `Mean daily maximum` )) +
  geom_crossbar(aes(y = `Mean daily minimum`)) +
  labs(x = 'Number', y = 'Temperature')

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...