【发布时间】:2018-06-29 11:53:38
【问题描述】:
我正在尝试绘制堆积条形图。它的工作,但仍然有一些我不明白的功能,例如,xts 做什么?我是否正在使用我加载的所有库?替换轴标签,它使用原始数据工作,但不适用于融化数据(数据被融化以产生堆叠条,因为我没有找到任何其他方法来使用 data.frame 产生堆叠条)。我还想为这个堆叠条使用单色。我尝试将 'fill = variable' 替换为 'fill = c("orange", "blue", "green")' 只是为了尝试,它不起作用。请帮忙..谢谢..
library(ggplot2)
library(xts)
library(reshape2)
library(lubridate)
library(zoo)
setwd("C:/Users/Hp/Documents/yr")
data1 <- read.csv("yr1983.csv", head = TRUE, stringsAsFactors = FALSE)
data1$Date <- dmy(data1$Date)
#data1 <- xts(x = data1[,-1], order.by = data1[,1])
head(data1)
Date Inland middle coastal
1 1983-11-01 0.0 0.0 0.0
2 1983-11-02 0.0 0.0 0.0
3 1983-11-03 90.5 19.5 60.0
4 1983-11-04 88.5 28.5 53.8
5 1983-11-05 80.5 73.0 122.0
6 1983-11-06 179.5 102.0 141.3
#plot stacked bar
data.m <- melt(data1,id.vars = "Date")
p1 <- ggplot(data.m, aes(x = Date, y = value,fill=variable)) +
geom_bar(stat='identity')
p1
#try to rename the axis - error
Rainfall_Intensity <- data1$value
month <- data1$Date
ggplot(data.m, aes(x = month, y = Rainfall_Intensity,fill= variable)) +
geom_bar(stat='identity')
*Error: Aesthetics must be either length 1 or the same as the data (276): x, y, fill
ggplot(data1, aes(month, y = Rainfall_Intensity,fill= variable)) + geom_bar(stat='identity')
*Error in eval(expr, envir, enclos) : object 'Date' not found
【问题讨论】: