как добавлять очки на сложенные барплоты - PullRequest
0 голосов
/ 21 мая 2018

Я хотел бы добавить точки (переменные виды nb) на мою уже существующую диаграмму.Я пытался добавить точки с помощью функции geom_point, но, к сожалению, у меня есть эта ошибка:

"Discrete value supplied to continuous scale".

Ниже вы найдете мои данные и мой код.

  structure(list(SOUNAME = c("BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)"
), year_month = c("2014-05", "2014-05", "2014-05", "2014-05", 
"2014-06", "2014-06", "2014-06", "2014-06", "2014-07", "2014-07", 
"2014-07", "2014-07"), pre_type = c("NONE", "HEAVY", "LIGHT", 
"MEDIUM", "NONE", "HEAVY", "LIGHT", "MEDIUM", "NONE", "HEAVY", 
"LIGHT", "MEDIUM"), pre_value = c(3, 6, 20, 2, 16, 2, 9, 2, 3, 
3, 22, 3), tem_type = c("V_COLD", "COLD", "HOT", "MEDIUM", "V_COLD", 
"COLD", "HOT", "MEDIUM", "V_COLD", "COLD", "HOT", "MEDIUM"), 
    tem_value = c(0, 31, 0, 0, 0, 24, 6, 0, 0, 23, 8, 0), nb_species = c("NA", 
    "3", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", 
    "NA"), x = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    3L)), .Names = c("SOUNAME", "year_month", "pre_type", "pre_value", 
"tem_type", "tem_value", "nb_species", "x"), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(data = complet_b, aes(x = x, y = pre_value, fill = pre_type), stat = "identity") +
          scale_x_continuous(breaks=1:3, labels=unique(complet_b$year_month)) +
          geom_bar(stat = "identity", width=0.3) +
          xlab("date") + ylab ("Number of days of precipitation") +
          ggtitle("Precipitation per month") + labs(fill = "Frequency") +
          geom_bar(data=complet_b,aes(x=x+0.4, y=tem_value, fill=tem_type), width=0.3, stat = "identity") +
          xlab("date") + ylab("Number of days of temperature") +
          ggtitle("Temperature per month") + labs(fill = "Frequency") 

Большое спасибо за то, что нашли время помочь мне !!

1 Ответ

0 голосов
/ 21 мая 2018

Ваш столбец nb_species представлен в символьном формате, преобразуйте его в числовой, и все готово.

Быстрый str(complet_b) скажет вам, что формат вашего столбца.

complet_b$nb_species <- as.numeric(complet_b$nb_species)

Вот ваш код и график с точкой на графике:

library(ggplot2)
ggplot(data = complet_b, aes(x = x, y = pre_value, fill = pre_type), stat = "identity") +
  scale_x_continuous(breaks=1:3, labels=unique(complet_b$year_month)) +
  geom_bar(stat = "identity", width=0.3) +
  xlab("date") + ylab ("Number of days of precipitation") +
  ggtitle("Precipitation per month") + labs(fill = "Frequency") +
  geom_bar(data=complet_b,aes(x=x+0.4, y=tem_value, fill=tem_type), width=0.3, stat = "identity") +
  xlab("date") + ylab("Number of days of temperature") +
  geom_point(aes(x = x, y= nb_species)) +
  ggtitle("Temperature per month") + labs(fill = "Frequency") 

enter image description here

...