Control time-series axis label and color in R stacked bar


Control time-series axis label and color in R stacked bar



enter image description here



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




3 Answers
3



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.





function rename cant be found, does it need special package?
– Siti Sal
Jun 29 at 15:45





Its a function of dplyr
– Tarssio Barreto
Jun 29 at 16:43



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


fill = variable


aes


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.





link you provide should be beneficial for me... still try to figure it out.. I tried your ggplot then it comeout with Error in +geom_bar(stat = "identity", fill = c("orange", "blue", "green")) : invalid argument to unary operator
– Siti Sal
Jun 29 at 15:52





I'm using + scale_fill_manual(values = c("thistle4", "violet", "blue")) and its work.. Thank you for your hint..
– Siti Sal
Jun 29 at 20:26





I'm using + scale_fill_manual(values = c("thistle4", "violet", "blue")) and its work.. Thank you for your hint..
– Siti Sal
Jun 29 at 20:26



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.


ggplot2


aes


data


ggplot


data


date


ggplot(data, aes(x = date, y = value))


data


date



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(...)).


names(data.m) <- c(...)


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.


labs



Changing several labels at once with labs (I just guessed based on the data sample):


labs


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:


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).





Very clear explanation, thank you...
– Siti Sal
Jun 29 at 15:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift