【发布时间】:2018-12-08 08:09:30
【问题描述】:
我正在尝试创建条形图,但有两个问题:
-
我想在每个栏下写下日期。我试过了,但它不起作用:
情节
-
我想让背景完全变白。 T 写了这个,但它也不起作用:
情节
情节
情节
这是我的 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)
【问题讨论】:
-
从图片来看,标签在那里但没有显示出来,因为它们太长了,占用太多空间。 in R base 的一个简单解决方案是
las,例如las = 3,它将标签横向翻转。ggplot中一定有类似的东西。