在ggplot中的每个条下添加标签

时间:2018-12-08 08:09:30

标签: r ggplot2

我正在尝试创建条形图,我有两个问题:

  1. 我想在每个栏中写下日期。我试过了,但是没用:

    图<-图+名称.arg = c(“ 31.07.2018”,“ 01.08.2018”,“ 02.08.2018”,“ 03.08.2018”,“ 06.08.2018”,“ 07.08.2018”, “ 08.08.2018”,“ 09.08.2018”,“ 13.08.2018”,“ 15.08.2018”,“ 17.08.2018”,“ 22.08.2018”,“ 23.08.2018”,“ 24.08.2018”,“ 25.08” .2018“)我

  2. 我希望背景完全为白色。 T写了这个,但是也不起作用:

    情节<-情节+主题(panel.background = element_rect(fill =“ white”))

    情节<-情节+主题(panel.grid.minor = element_blank())

    图<-图+主题(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank())

这是我的R脚本:

options(stringsAsFactors = FALSE)

input1 <- "C:\\Users\\number_sightings.csv"

number_sightings <- read.csv(input1, sep=";")
number_sightings<-structure(list(date = c("31.07.2018", "01.08.2018", "02.08.2018", "03.08.2018", "06.08.2018", "07.08.2018", "08.08.2018", "09.08.2018", "13.08.2018", "15.08.2018", "17.08.2018", "22.08.2018", "23.08.2018", "24.08.2018", "25.08.2018"), number = c(2.7, 0.99, 2.11, 1.63, 1.16, 1, 3.57, 1, 1.84, 3.25, 2.25, 2, 1.88, 2.67, 3.04)), class = "data.frame", row.names = c(NA, -15L))

number_sightings$date = as.Date(number_sightings$date, format = "%d.%m.%Y")

library(lubridate)

library(ggplot2)

library(scales)

dput(number_sightings)

plot <-ggplot(number_sightings, aes(x=date, y=number)) + geom_bar(stat="identity")

plot <- plot + names.arg=c("31.07.2018", "01.08.2018", "02.08.2018", "03.08.2018", "06.08.2018", "07.08.2018", "08.08.2018", "09.08.2018", "13.08.2018", "15.08.2018", "17.08.2018", "22.08.2018", "23.08.2018", "24.08.2018", "25.08.2018")

plot <- plot+ theme(axis.text = element_text(size = 8, colour="black", angle=90)) 

plot <- plot + geom_text(aes(label=round(number,digits = 2)), vjust=-0.5) 

plot <- plot + scale_x_date(breaks=seq("30.07.18, 02.09.18"),expand=c(0,0), date_labels=("%d.%m.%y"), date_breaks = "2 day")

plot <- plot +  theme(panel.background = element_rect(fill = "white"))

plot <- plot + theme(panel.grid.minor=element_blank())

plot <- plot + theme(panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank())

plot <- plot+ scale_y_continuous(limits=c(0,4), breaks=seq(0, 4, 0.5), expand=c(0,0))

plot<- plot +theme(axis.text.x = element_text(angle=65, vjust=0.6))

plot <-plot +theme(legend.position="none") + theme_bw()

print(plot)

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是我认为您要查找的内容的最小示例:

library(ggplot2)

number_sightings <- structure(
  list(
    date = c(
      "31.07.2018", "01.08.2018", "02.08.2018", "03.08.2018",
      "06.08.2018", "07.08.2018", "08.08.2018", "09.08.2018",
      "13.08.2018", "15.08.2018", "17.08.2018", "22.08.2018",
      "23.08.2018", "24.08.2018", "25.08.2018"
    ),
    number = c(
      2.7, 0.99, 2.11, 1.63, 1.16, 1, 3.57, 1, 1.84, 3.25, 2.25,
      2, 1.88, 2.67, 3.04
    )
  ),
  class = "data.frame", row.names = c(NA, -15L)
)

number_sightings$date <- as.Date(number_sightings$date, format = "%d.%m.%Y")

ggplot(number_sightings, aes(x = date, y = number)) +
  geom_bar(stat = "identity") +
  scale_x_date(breaks = number_sightings$date) +
  theme(
    panel.background = element_rect(fill = NA),
    axis.text.x = element_text(angle = 90)
  )

reprex package(v0.2.0.9000)于2018-12-08创建。