I'd like to reproduce the graph in the attached figure in R. Note that the x-axis contains categorical variables (hour and day). There could be multiple lines in the y-axis (eg, the column ETR). I had to delete some entries for the day, otherwise Excel would repeat it in the lower layer (maybe there's a way to avoid having to do that in Excel?).
Thank you.
EDIT
The original database file is here .
I would not say that your x axis is mapped from a categorical variable. Rather, the plot looks like a straightforward time series plot to me. The steps I'd take to reproduce it are probably: 1) read in date, time, and y value. 2) fill in "missing" dates using something like the zoo package's na.locf function. 3) merge date and time into a datetime column, possibly using as.POSIXlt(paste(...), format=...). 4) create time series with zoo package if data needs to be manipulated. 5) plot using plain R or ggplot.
Here's a possible approach to solve the problem based on the comments posted so far. There are still some things missing that I don't know how to do. For example, the two layers in the x-axis as shown in the Excel chart (one layer for time and one for day). also, the part of slicing the data produces a row with NAs, not sure why.
Thanks a lot for the help!
EDIT:
Fixed code and added "scales" suggestion.
## Read data
data <- read.csv("http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/data/tmy3/722287TYA.CSV",skip=1,header=TRUE)
## Construct data frame (add any columns as needed)
data.df <- data.frame(ts=as.POSIXct(paste(data$Date..MM.DD.YYYY.,data$Time..HH.MM.),format = "%m/%d/%Y %H:%M"),
GHI=data$GHI..W.m.2.,
GHIunc=data$GHI.uncert....)
## Window of data
ts.start <- as.POSIXct("1/1/1991 1:00",format="%m/%d/%Y %H:%M")
ts.end <- as.POSIXct("1/5/1991 1:00",format="%m/%d/%Y %H:%M")
## Slice data
data.df.window <- data.df[data.df$ts >= ts.start,]
data.df.window <- data.df.window[data.df.window$ts <= ts.end,]
row.names(data.df.window) <- NULL
## Example of plotting
library(ggplot2)
library(scales)
# Uncertainty in GHI data
GHI.lo <- data.df.window$GHI*(1 - data.df.window$GHIunc/100)
GHI.up <- data.df.window$GHI*(1 + data.df.window$GHIunc/100)
m <- ggplot(data.df.window,aes(x=data.df.window$ts,y=data.df.window$GHI)) +
geom_line() +
labs(title = "Global Horizontal Irradiance (Wh/m^2)",x="Date",y="") +
theme(text = element_text(size=20)) +
geom_ribbon(data=data.df.window,aes(ymin=GHI.lo,ymax=GHI.up),alpha=0.3,fill='blue') +
scale_x_datetime(labels=date_format("%m/%d/%Y"),breaks=date_breaks("1 day"),minor_breaks=date_breaks("1 hour"))
print(m)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.