在x轴ggplot上添加其他bin标签

时间:2017-11-16 14:32:55

标签: r ggplot2

我正在寻求创造一个情节。这是我到目前为止所得到的:

enter image description here

我想这样做,所以我的每个箱子都有它自己的标签。

我试过了:

  p1<-ggplot(data, aes(x=Onset)) +
  geom_histogram(binwidth= 1)+
  labs (x= "Date of Onset of Senescence (Julian)", y="# of Trees")

p1 + scale_y_continuous(breaks=c(270, 272, 274, 276, 278, 280,282,284,286,288,290, 292,294,296,298,300))

但那不适合我。请查看我的数据:

> dput(data)
structure(list(ï..Tree = structure(c(1L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 2L, 3L, 12L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 13L, 14L, 23L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 24L, 
25L), .Label = c("A1", "A10", "A11", "A2", "A3", "A4", "A5", 
"A6", "A7", "A8", "A9", "B1", "B10", "B11", "B2", "B3", "B4", 
"B5", "B6", "B7", "B8", "B9", "C1", "C10", "C11", "C2", "C3", 
"C4", "C5", "C6", "C7", "C8", "C9"), class = "factor"), SourceLocation = structure(c(3L, 
3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L, 7L, 
7L, 7L, 6L, 6L, 6L, 2L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 6L
), .Label = c("Brooke Township", "Delaware Township", "Dunwich Township", 
"Mosa Township", "Pelee Island", "Sombra Township", "Yarmouth Township"
), class = "factor"), Onset = c(279L, 279L, 285L, 283L, 283L, 
275L, 277L, 287L, 287L, 295L, 285L, 271L, 283L, 269L, 275L, 285L, 
285L, 285L, 285L, 285L, 287L, 283L, 279L, 293L, 283L, 285L, 285L, 
283L, 279L, 279L, 279L, 277L, 279L), Completion = c(289L, 287L, 
299L, 299L, 295L, 287L, 287L, 299L, NA, NA, NA, 285L, 299L, 285L, 
287L, NA, NA, NA, NA, 295L, NA, 299L, 295L, NA, 295L, 299L, 299L, 
293L, 297L, 299L, 295L, 295L, 299L), Duration = c(10L, 8L, 14L, 
16L, 12L, 12L, 10L, 12L, NA, NA, NA, 14L, 16L, 16L, 12L, NA, 
NA, NA, NA, 10L, NA, 16L, 16L, NA, 14L, 14L, 14L, 10L, 18L, 20L, 
16L, 18L, 20L)), .Names = c("ï..Tree", "SourceLocation", "Onset", 
"Completion", "Duration"), row.names = c(NA, 33L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

首先要创建自己的bin:

library(dplyr)
library(ggplot)
data %>% 
  count(Onset) %>% 
  mutate_at("Onset", factor) %>% 
  ggplot(aes(Onset, n)) + 
  geom_bar(stat = "identity") +
  labs (x= "Date of Onset of Senescence (Julian)", y="# of Trees")