Reputation: 119
I'm trying to plot a stacked bar. Its work but still there are some function that I don't understand e.g, what xts do? Am I using all the library I've load? Replacing the axis label, its work with original data, but not with melted data (data was melted for producing stacked bar, because I didn't find any other ways to produced stacked bar using data.frame) . I also want to use monochrome color for this stacked bar. I try replacing 'fill = variable' to 'fill = c("orange", "blue", "green")' just to try, its not working. Kindly help.. Thank you..
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
Upvotes: 0
Views: 774
Reputation: 16832
ggplot2
operates on entire data frames, so it expects that whatever names you use to map to aesthetics in aes
are the bare column names from the data frame supplied to either the data
param of the initial ggplot
call, or a data
param for a specific geom. Therefore, if you have a global variable called date
and you call ggplot(data, aes(x = date, y = value))
, it will be looking for a column in data
called date
, and will throw an error if one isn't found.
If you need to rename columns in your data frame, you can do that lots of different ways, such as names(data.m) <- c(...)
or setNames(data.m, c(...))
.
But if all you need to do is change the axis labels, you can do that as part of building the plot. Either assign labels using labs
, or assign a single label within the corresponding scale function.
Changing several labels at once with labs
(I just guessed based on the data sample):
library(tidyverse)
...
ggplot(data.m, aes(x = Date, y = value, fill = variable)) +
geom_col() +
labs(x = "Month", y = "Rainfall intensity", fill = "Location",
title = "Rainfall intensity by location",
subtitle = "November 1983")
Changing just the x-axis label within a call to scale_x_date
:
ggplot(data.m, aes(x = Date, y = value, fill = variable)) +
geom_col() +
scale_x_date(name = "Month")
Created on 2018-06-29 by the reprex package (v0.2.0).
Upvotes: 1
Reputation: 861
fill = variable
under aes
is referring to the variable according to which the stacked bars are supposed to be separated. To change the colours of the stacked bars, you want to change fill under geom_bar
ggplot(data.m, aes(x = Date, y = value,fill=variable))
+ geom_bar(stat='identity', fill = c("orange", "blue", "green"))
You can refer to - http://sape.inf.usi.ch/quick-reference/ggplot2/colour - for choosing colours.
Upvotes: 1
Reputation: 40
look that:
Rainfall_Intensity <- data1$value
month <- data1$Date
The variables Rainfall_Intensity and month they not inside of the data.m. Therefore, when you use ggplot it generates the errors presented above. You must rename the variables:
rename(data.m,Rainfall_Intensity = value, month = Date)
And, after this, run your ggplot2.
Upvotes: 1