ggplot - error: no layers in plot - r

I am having a real hard time with ggplot function!
I try to briefly explain my problem.
I have a dataset of several tweets associated to a time stamp; I would like to plot the data obtaining a graph with time on the x bar and the frequency or the "tweet-rate" per hour on the y axis.
What did I do?
library(ggplot2)
c4l.tweets <- read.csv("/Users/vincenzo/Desktop/Collect %23c4l13 Tweets - Archive.csv")
c4l.tweets$time <- as.POSIXct(strptime(c4l.tweets$time, "%d/%m/%Y %H:%M:%S", tz="CST") - 6*60*60)
library(chron)
c4l.tweets$by.hour <- trunc(c4l.tweets$time, units="hours")
ggplot(count(c4l.tweets, "by.hour"), aes(x=by.hour, y=freq))
+ geom_bar(stat="identity") + xlab("Number") + ylab("Date") + labs(title="tweets by hour")
So basically I truncated the data by the timestamp and used the count function to plot them.
I get the
Error: No layers in plot
and
Error in +geom_bar(stat = "identity") : argument not valid for the operator
But why? what am I doing wrong?
I usually have this problem each time i try to plot something via ggplot, what do I do wrong?
Thank you!
Vincenzo

Related

R ggplot: eliminating empty date range from time series plot?

On the bottom image, I have a graph produced by this code:
library(lubridate)
shangPM$date <- with(shangPM, ymd_h(paste(year, month, day, hour, sep= ' ')))
ggplot(data = shangPM, aes(x = date, y = PM_US.Post)) +
geom_line()
However, there is four years shown on my x-axis with no data, making the graph look weird. I tried using xlim and coord_cartesian, but this does not seem to be working well with my date variable (maybe I'm wrong?)
A bit of a noob here - can someone help me zoom in on only the dates I have data for for my plot?
Here is my error:
Error in as.POSIXct.numeric(value) : 'origin' must be supplied

Barchart with ggplot 2 y axis labels

I have a little problem with a ggplot barchart.
I wanted to make a barchart with ggplot2 in order to compare my Svolumes for my 4 stocks on a period of few months.
I have two problems:
The first one is that my y axis is wrong. My graph/data seems correct but the y axis don't "follow" as I thought it will contain another scale... I would to have to "total" number of my dataset svolumes, I think here it is writing my svolumes values. I don't know how to explain but I would like the scale corresponding to all of my data on the graph like 10,20,etc until my highest sum of svolumes.
There is my code:
Date=c(rep(data$date))
Subject=c(rep(data$subject))
Svolume=c(data$svolume)
Data=data.frame(Date,Subject,Svolume)
Data=ddply(Data, .(Date),transform,pos=cumsum(as.numeric(Svolume))-(0.5*(as.numeric(Svolume))))
ggplot(Data, aes(x=Date, y=Svolume))+
geom_bar(aes(fill=Subject),stat="identity")+
geom_text(aes(label=Svolume,y=pos),size=3)
and there is my plot:
I helped with the question here
Finally, How could I make the same plot for each months please? I don't know how to get the values per month in order to have a more readable barchart as we can't read anything here...
If you have other ideas for me I would be very glad to take any ideas and advices! Maybe the same with a line chart would be more readable...? Or maybe the same barchart for each stocks ? (I don't know how to get the values per stock either...)
I just found how to do it with lines.... but once again my y axis is wrong, and it's not very readable....
Thanks for your help !! :)
Try adding the following line right before your ggplot function. It looks like your y-axis is in character.
[edit] Incorporate #user20650's comments, add as.character() first then convert to numeric.
Data$Svolume <- as.numeric(as.character(Data$Svolume))
To produce the same plot for each month, you can add the month variable first: Data$Month <- month(as.Date(Date)). Then add facet to your ggplot object.
ggplot(Data, aes(x=Date, y=Svolume) +
...
+ facet_wrap(~ Month)
For example, your bar chart code will be:
Data$Svolume <- as.numeric(as.character(Data$Svolume))
Data$Month <- month(as.Date(Date))
ggplot(Data, aes(x=Date, y=Svolume)) +
geom_bar(aes(fill=Subject),stat="identity") +
geom_text(aes(label=Svolume,y=pos),size=3) +
facet_wrap(~ Month)
and your Line chart code will be:
Data$Svolume <- as.numeric(as.character(Data$Svolume))
Data$Month <- month(as.Date(Date))
ggplot(Data, aes(x=Date, y=Svolume, colour=Subject)) +
geom_line() +
facet_wrap(~ Month)

Problems creating datetime series graph in R using ggplot

I am trying to create a graph with the following characteristics:
x-axis: time and date
y-axis: data
here you can download my dataframe: https://my.cloudme.com/josechka/data
I try to produce the graph using:
p <- ggplot(data,aes(x = Date, y = Var,group = 1))
+ geom_line()
+ scale_x_date(labels = date_format("%m/%d/%Y"))
+ scale_y_continuous(limits = c(0, 70000))
p
And I get the result:
Error: Invalid input: date_trans works with objects of class Date only
I am quite new in R and ggplot. What am I doing wrong?
As suggested you have to format the Date column into a Date object.
data$Date<-as.Date(data$Date, format="%d/%m/%Y")
Now you can use your script in order to create the plot:
library("ggplo2")
library("scales")
p <- ggplot(data,aes(x = Date, y = Var,group = 1))
+ geom_line()
+ scale_x_date(labels = date_format("%m/%d/%Y"))
+ scale_y_continuous(limits = c(0, 70000))
p
And this is the resulting plot:
Thanks for the comments. They helped me to find out the solution. Both comments allow to represent my data. However, there is small problem: data from the same day is grouped and it is not possible to see the daily behaviour of the variable. I tested to format the Date column using the next command:
as.POSIXct(data$Date, format="%d/%m/%Y %H:%M:%S")
It worked out. However it is important to have the original data in the format d/m/Y h:m:s. Thanks very much for the comments which help me a lot to solve my problem.

Modifying Plot in ggplot2 using as.yearmon from zoo

I have created a graph in ggplot2 using zoo to create month bins. However, I want to be able to modify the graph so it looks like a standard ggplot graph. This means that the bins that aren't used are dropped and the bins that are populate the entire bin space. Here is my code:
library(data.table)
library(ggplot2)
library(scales)
library(zoo)
testset <- data.table(Date=as.Date(c("2013-07-02","2013-08-03","2013-09-04","2013-10-05","2013-11-06","2013-07-03","2013-08-04","2013-09-05","2013-10-06","2013-11-07")),
Action = c("A","B","C","D","E","B","A","B","C","A","B","E","E","C","A"),
rating = runif(30))
The ggplot call is:
ggplot(testset, aes(as.yearmon(Date), fill=Action)) +
geom_bar(position = "dodge") +
scale_x_yearmon()
I'm not sure what I'm missing, but I'd like to find out! Thanks in advance!
To get a "standard-looking" plot, convert the data to a "standard" data type, which is a factor:
ggplot(testset, aes(as.factor(as.yearmon(Date)), fill=Action)) +
geom_bar(position='dodge')

How to deal with "data of class uneval" error from ggplot2?

While trying to overlay a new line to a existing ggplot, I am getting the following error:
Error: ggplot2 doesn't know how to deal with data of class uneval
The first part of my code works fine. Below is an image of "recent" hourly wind generation data from a Midwestern United States electric power market.
Now I want to overlay the last two days worth of observations in Red. It should be easy but I cant figure out why I am getting a error.
Any assistance would be greatly appreciated.
Below is a reproducible example:
# Read in Wind data
fname <- "https://www.midwestiso.org/Library/Repository/Market%20Reports/20130510_hwd_HIST.csv"
df <- read.csv(fname, header=TRUE, sep="," , skip=7)
df <- df[1:(length(df$MKTHOUR)-5),]
# format variables
df$MWh <- as.numeric(df$MWh)
df$Datetime <- strptime(df$MKTHOUR, "%m/%d/%y %I:%M %p")
# Create some variables
df$Date <- as.Date(df$Datetime)
df$HrEnd <- df$Datetime$hour+1
# Subset recent and last data
last.obs <- range(df$Date)[2]
df.recent <- subset(df, Date %in% seq(last.obs-30, last.obs-2, by=1))
df.last <- subset(df, Date %in% seq(last.obs-2, last.obs, by=1))
# plot recent in Grey
p <- ggplot(df.recent, aes(HrEnd, MWh, group=factor(Date))) +
geom_line(color="grey") +
scale_y_continuous(labels = comma) +
scale_x_continuous(breaks = seq(1,24,1)) +
labs(y="MWh") +
labs(x="Hour Ending") +
labs(title="Hourly Wind Generation")
p
# plot last two days in Red
p <- p + geom_line(df.last, aes(HrEnd, MWh, group=factor(Date)), color="red")
p
when you add a new data set to a geom you need to use the data= argument. Or put the arguments in the proper order mapping=..., data=.... Take a look at the arguments for ?geom_line.
Thus:
p + geom_line(data=df.last, aes(HrEnd, MWh, group=factor(Date)), color="red")
Or:
p + geom_line(aes(HrEnd, MWh, group=factor(Date)), df.last, color="red")
Another cause is accidentally putting the data=... inside the aes(...) instead of outside:
RIGHT:
ggplot(data=df[df$var7=='9-06',], aes(x=lifetime,y=rep_rate,group=mdcp,color=mdcp) ...)
WRONG:
ggplot(aes(data=df[df$var7=='9-06',],x=lifetime,y=rep_rate,group=mdcp,color=mdcp) ...)
In particular this can happen when you prototype your plot command with qplot(), which doesn't use an explicit aes(), then edit/copy-and-paste it into a ggplot()
qplot(data=..., x=...,y=..., ...)
ggplot(data=..., aes(x=...,y=...,...))
It's a pity ggplot's error message isn't Missing 'data' argument! instead of this cryptic nonsense, because that's what this message often means.
This could also occur if you refer to a variable in the data.frame that doesn't exist. For example, recently I forgot to tell ddply to summarize by one of my variables that I used in geom_line to specify line color. Then, ggplot didn't know where to find the variable I hadn't created in the summary table, and I got this error.

Resources