цвета gom-line и geom_bar - PullRequest
       32

цвета gom-line и geom_bar

0 голосов
/ 06 марта 2019

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

Любая помощь приветствуется!

вот мой код:

windows()
ggplot(data = rr, aes(x = date))+
  geom_point(aes(y=value, colour="Nitrogen"))+
  geom_line(aes(y=value, colour="Nitrogen"))+
  facet_grid(depth~dose) +
    geom_bar(data=rr,stat = "identity", position = position_dodge(width = 0.9),aes(x=date, y=rain100 ,colour="Rains"))+
  theme(strip.text.x = element_text(size = 7, colour = "black", angle = 0),axis.text.x = element_text(angle=90,vjust=0.5,hjust=0.5, size=8))+
  ylab("Soil nitrogen measured as nitrate, lb/ac and rains, 100ths of inch")+
  scale_x_date(date_breaks = "1 month",date_labels = "%m-%d-%y")+
  ggtitle("MR Ranch")+
  ylim(0,350)+
  scale_colour_manual("",
                      breaks=c("Nitrogen","Rains"),
                      values=c("black","blue"))

вот (сокращенная версия) моего набора данных

    structure(list(date = structure(c(17444, 17444, 17444, 17444, 
17456, 17456, 17456, 17456, 17457, 17457, 17457, 17457, 17473, 
17473, 17473, 17473, 17485, 17485, 17485, 17485, 17508, 17508, 
17508, 17508, 17550, 17550, 17550, 17550), class = "Date"), depth = c("12-24 in", 
"12-24 in", "0-12 in", "0-12 in", "12-24 in", "0-12 in", "0-12 in", 
"12-24 in", "12-24 in", "12-24 in", "0-12 in", "0-12 in", "0-12 in", 
"12-24 in", "0-12 in", "12-24 in", "0-12 in", "12-24 in", "0-12 in", 
"12-24 in", "12-24 in", "0-12 in", "0-12 in", "12-24 in", "12-24 in", 
"0-12 in", "0-12 in", "12-24 in"), value = c(60, 60, 60, 60, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 50, 20, 12, 69.5, 
87, 30, 347, 12, 35, 17.4, 35, 35), trt = c("Experiment", "Control", 
"Control", "Experiment", "Control", "Control", "Experiment", 
"Experiment", "Experiment", "Control", "Control", "Experiment", 
"Experiment", "Experiment", "Control", "Control", "Experiment", 
"Control", "Control", "Experiment", "Experiment", "Control", 
"Experiment", "Control", "Experiment", "Control", "Experiment", 
"Control"), dose = c("High Preplant", "Low/No Preplant", "Low/No Preplant", 
"High Preplant", "Low/No Preplant", "Low/No Preplant", "High Preplant", 
"High Preplant", "High Preplant", "Low/No Preplant", "Low/No Preplant", 
"High Preplant", "High Preplant", "High Preplant", "Low/No Preplant", 
"Low/No Preplant", "High Preplant", "Low/No Preplant", "Low/No Preplant", 
"High Preplant", "High Preplant", "Low/No Preplant", "High Preplant", 
"Low/No Preplant", "High Preplant", "Low/No Preplant", "High Preplant", 
"Low/No Preplant"), rain = c(0, 0, 0, 0, 0, 0, 0, 0, 0.01, 0.01, 
0.01, 0.01, 0.09, 0.09, 0.09, 0.09, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 
0.03, 0.03, 0.03), rain100 = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
1, 1, 9, 9, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3)), row.names = c(NA, 
-28L), class = "data.frame")

Here is what I get

Ответы [ 2 ]

1 голос
/ 06 марта 2019

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

библиотека (ggplot2)

ggplot(data = rr, aes(x = date))+
  geom_point(aes(y=value, fill="Nitrogen"), colour="black")+
  geom_line(aes(y=value,  fill="Nitrogen"), colour="black")+
  facet_grid(depth~dose) +
  geom_bar(data=rr,stat = "identity", position = position_dodge(width = 0.9),aes(x=date, y=rain100, fill="Rains") )+
  theme(strip.text.x = element_text(size = 7, colour = "black", angle = 0),axis.text.x = element_text(angle=90,vjust=0.5,hjust=0.5, size=8))+
  ylab("Soil nitrogen measured as nitrate, lb/ac and rains, 100ths of inch")+
  scale_x_date(date_breaks = "1 month",date_labels = "%m-%d-%y")+
  ggtitle("MR Ranch")+
  ylim(0,350) +
  scale_fill_manual(labels=c("Nitrogen","Rains"), breaks=c("Nitrogen","Rains"), values=c("black","blue"), name="") 

enter image description here

1 голос
/ 06 марта 2019

Похоже, вы хотите гистограмму, а не график. Поэтому я изменил geom_bar на geom_histogram. Кроме того, я полагаю, что вы получаете эту легенду, потому что вы используете аргумент color вместо fill, который даст вам схему. Попробуйте этот код:

ggplot(data = rr, aes(x = date)) +
  geom_point(aes(y = value, colour = "Nitrogen")) +
  geom_line(aes(y = value, colour = "Nitrogen")) +
  facet_grid(depth ~ dose) +
  geom_histogram(data= rr, stat = "identity", position = position_dodge(width = 0.9), aes(x = date, y = rain100 ,fill= "Rains")) +
  theme(strip.text.x = element_text(size = 7, colour = "black", angle = 0), axis.text.x = element_text(angle = 90,vjust = 0.5, hjust = 0.5, size = 8)) +
  ylab("Soil nitrogen measured as nitrate, lb/ac and rains, 100ths of inch") +
  scale_x_date(date_breaks = "1 month", date_labels = "%m-%d-%y") +
  ggtitle("MR Ranch")+
  ylim(0,350)+
  scale_colour_manual("",
                      breaks=c("Nitrogen","Rains"),
                      values=c("black","blue"))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...