Multiple plots in R with time series - r

enter image description hereI have the following data; please can any one help me to plot it, I have tried to use a lot of different commands but none has given me a perfect graph
year x y
2012 4 5
2014 7 9
2017 4 3
enter image description here
this picture i need to make as it

Based on your comments you might be looking for:
library(tidyverse)
plot1 <- df %>% gather(key = measure, value = value, -year) %>%
ggplot(aes(x = year, y = value, color = measure))+
geom_point()+
geom_line()+
facet_wrap(~measure)
plot1
The biggest points here are gather and facet_wrap. I recommend the following two links:
https://ggplot2.tidyverse.org/reference/facet_grid.html
https://ggplot2.tidyverse.org/reference/facet_wrap.html

You need to convert year column type to Date.
This is a tidyverse style solution
library(tidyverse)
mydf %>%
rename("col1" = x, "col2" = y) %>%
mutate(year = paste0(year, "-01-01")) %>%
mutate(year = as.Date(year)) %>%
ggplot() +
geom_line(aes(x = year, y = col1), color = "red", size = 2) +
geom_line(aes(x = year, y = col2), color = "blue", size = 2) +
theme_minimal()
which returns this

Using the data shown reproducibly in the Note below use matplot. No packages are used.
matplot(dd[[1]], dd[-1], pch = c("x", "y"), type = "o", xlab = "year", ylab = "value")
Note
dd <- structure(list(year = c(2012L, 2014L, 2017L), x = c(4L, 7L, 4L),
y = c(5L, 9L, 3L)), class = "data.frame", row.names = c(NA, -3L))

Related

R Plotting three timeseries in two facet_grids in ggplot

Is it possible to plot three timeseries in only two grids using ggplot and facet_grid()?
# Create some fake data
stock1 = cumprod(1+c(0, rnorm(99, 0, .05)))
stock2 = cumprod(1+c(0, rnorm(99, 0, .075)))
indicator = sample(1:50, 100, replace = TRUE)
date_seq = seq.Date(as.Date("2023-01-01"), length.out = 100, by = 1)
df = data.frame(date = date_seq, stock1 = stock1, stock2 = stock2, indicator = indicator)
Now I would like to see an upper graph with the two stocks and one lower graph with the indicator using facet_grid().
The only result I get is a three-grid plot
grid_df = pivot_longer(df, c(stock1, stock2, indicator), names_to = "underlying", values_to = "values")
ggplot(grid_df, aes(x = date, y = values, colour = underlying)) +
geom_line() +
facet_grid(vars(underlying), scales = "free")
I dont know how to group the two stocks to bring them into one grid.
Thanks for help!
You could add an extra column to your longer format data where you could combine the stocks 1 and 2 to one string called stocks and leave the indicator alone using an ifelse to assign them to the facet_grid like this:
library(ggplot2)
library(dplyr)
library(tidyr)
grid_df = pivot_longer(df, c(stock1, stock2, indicator), names_to = "underlying", values_to = "values") %>%
mutate(grids = ifelse(underlying == "indicator", "indicator", "stock"))
ggplot(grid_df, aes(x = date, y = values, colour = underlying)) +
geom_line() +
facet_grid(vars(grids), scales = "free")
Created on 2023-02-19 with reprex v2.0.2

plotting all columns of a dataframe in r per grouping variable onto one single plot

D
Dear all, given the following dataframe, I am trying to plot the values for each column and marked overall (coloured) by type (there are three groups, alpha, beta, gamma).
In other words the x axis should display 9 points (x100,x110....,x180) and the y axis range should be from 0 to 2 (each column takes a value from 0 to 2 max).
Each of the resulting three lines should highlight each of the three categorical variables.
Apologies about the format of the dataframe, I have not figured out how to pretty output it yet.
structure(list(Group.1 = c("alpha", "beta", "gamma"), x100 =
c(1.31729175522923,
0.985278656706214, 0.156200287397951), x110 = c(1.54471416538581,
0.915659603197128, 0.733224726747721), x120 = c(1.27778739808127,
0.813037838321179, 0.779596480540931), x130 = c(1.25000598328188,
0.488610395696014, 0.806707685347646), x140 = c(1.82296009687707,
1.16132276877761, 1.31973652262241), x150 = c(0.929914232343435,
1.41477890312672, 1.41652730805799), x160 = c(1.19612871715799,
0.801679770927876, 0.39746836386621), x170 = c(1.88860023999587,
1.03295020200312, 0.729622524231672), x180 = c(0.926427994389087,
1.20304362708703, 1.57529754098505)), row.names = c(NA, -3L),
class = "data.frame")
I am trying to use ggplot (any other method of plotting would do) but in reading the requirements of the ggplot function I am struggling to understand if I should possibly create a vector of values to use as the aes parameter y?
Thanks in advance,
F
To get the layout you need for plotting you need to convert your data.frame to a long format using pivot_longer. From there you can just use ggplot as normal. To unstack the bars use position = dodge.
library(tidyverse)
data <- structure(list(Group.1 = c("alpha", "beta", "gamma"),
x100 = c(1.31729175522923, 0.985278656706214, 0.156200287397951),
x110 = c(1.54471416538581, 0.915659603197128, 0.733224726747721),
x120 = c(1.27778739808127, 0.813037838321179, 0.779596480540931),
x130 = c(1.25000598328188, 0.488610395696014, 0.806707685347646),
x140 = c(1.82296009687707, 1.16132276877761, 1.31973652262241),
x150 = c(0.929914232343435, 1.41477890312672, 1.41652730805799),
x160 = c(1.19612871715799, 0.801679770927876, 0.39746836386621),
x170 = c(1.88860023999587, 1.03295020200312, 0.729622524231672),
x180 = c(0.926427994389087, 1.20304362708703, 1.57529754098505)),
row.names = c(NA, -3L),
class = "data.frame")
data %>%
pivot_longer(cols = contains("x"),
names_to = "data_points",
values_to = "vals") %>%
ggplot(aes(x = data_points, y = vals, fill = Group.1)) +
geom_col(position = "dodge")
Created on 2020-11-11 by the reprex package (v0.3.0)
A base-R solution would be to reshape the data to long format with reshape before plotting.
dflong <- reshape(df, direction = "long", idvar = "Group.1", timevar = "xval",
varying = 2:10, v.names = "yval", times = colnames(df)[2:10])
library(ggplot2)
ggplot(data = dflong, aes(x = xval, y = yval, color = Group.1)) +
geom_line(aes(group = Group.1))

Spaghetti plot using ggplot in R?

I would like to produce a speghatii plot where i need to see days of the year on the x-axis and data on the y-axis for each Year. I would then want a separate year that had data for only 3 months (PCPNewData) to be plotted on the same figure but different color and bold line. Here is my sample code which produce a graph (attached) where the data for each Year for a particular Day is stacked- i don't want bar graph. I would like to have a line graph. Thanks
library(tidyverse)
library(tidyr)
myDates=as.data.frame(seq(as.Date("2000-01-01"), to=as.Date("2010-12-31"),by="days"))
colnames(myDates) = "Date"
Dates = myDates %>% separate(Date, sep = "-", into = c("Year", "Month", "Day"))
LatestDate=as.data.frame(seq(as.Date("2011-01-01"), to=as.Date("2011-03-31"),by="days"))
colnames(LatestDate) = "Date"
NewDate = LatestDate %>% separate(Date, sep = "-", into = c("Year", "Month", "Day"))
PCPDataHis = data.frame(total_precip = runif(4018, 0,70), Dates)
PCPNewData = data.frame(total_precip = runif(90, 0,70), NewDate)
PCPDataHisPlot =PCPDataHis %>% group_by(Year) %>% gather(key = "Variable", value = "Value", -Year, -Day,-Month)
ggplot(PCPDataHisPlot, aes(Day, Value, colour = Year))+
geom_line()+
geom_line(data = PCPNewData, aes(Day, total_precip))
I would like to have a Figure like below where each line represent data for a particular year
UPDATE:
I draw my desired figure with hand (see attached). I would like to have all the days of the Years on x-axis with its data on the y-axis
You have few errors in your code.
First, your days are in character format. You need to pass them in a numerical format to get line being continuous.
Then, you have multiple data for each days (because you have 12 months per year), so you need to summarise a little bit these data:
Pel2 <- Pelly2Data %>% group_by(year,day) %>% summarise(Value = mean(Value, na.rm = TRUE))
Pel3 <- Pelly2_2011_3months %>% group_by(year, day) %>% summarise(total_precip = mean(total_precip, na.rm = TRUE))
ggplot(Pel2, aes(as.numeric(day), Value, color = year))+
geom_line()+
geom_line(data = Pelly2_2011_3months, aes(as.numeric(day), y= total_precip),size = 2)
It looks better but it is hard to apply a specific color pattern
To my opinion, it will be less confused if you can compare mean of each dataset, such as:
library(tidyverse)
Pel2 <- Pelly2Data %>% group_by(day) %>%
summarise(Mean = mean(Value, na.rm = TRUE),
SEM = sd(Value,na.rm = TRUE)/sqrt(n())) %>%
mutate(Name = "Pel_ALL")
Pel3 <- Pelly2_2011_3months %>% group_by(day) %>%
summarise(Mean = mean(total_precip, na.rm = TRUE),
SEM = sd(total_precip, na.rm = TRUE)/sqrt(n())) %>%
mutate(Name = "Pel3")
Pel <- bind_rows(Pel2,Pel3)
ggplot(Pel, aes(x = as.numeric(day), y = Mean, color = Name))+
geom_ribbon(aes(ymin = Mean-SEM, ymax = Mean+SEM), alpha = 0.2)+
geom_line(size = 2)
EDIT: New graph based on update
To get the graph you post as a drawing, you need to have the day of the year and not the day of the month. We can get this information by setting a date sequence and extract the day of the year by using yday function from `lubridate package.
library(tidyverse)
library(lubridate)
Pelly2$Date = seq(ymd("1990-01-01"),ymd("2010-12-31"), by = "day")
Pelly2$Year_day <- yday(Pelly2$Date)
Pelly2_2011_3months$Date <- seq(ymd("2011-01-01"), ymd("2011-03-31"), by = "day")
Pelly2_2011_3months$Year_day <- yday(Pelly2_2011_3months$Date)
Pelly2$Dataset = "ALL"
Pelly2_2011_3months$Dataset = "2011_Dataset"
Pel <- bind_rows(Pelly2, Pelly2_2011_3months)
Then, you can combine both dataset and represent them with different colors, size, transparency (alpha) as show here:
ggplot(Pel, aes(x = Year_day, y = total_precip, color = year, size = Dataset, alpha = Dataset))+
geom_line()+
scale_size_manual(values = c(2,0.5))+
scale_alpha_manual(values = c(1,0.5))
Does it answer your question ?

How do I plot the differences between two groups, across multiple sampling days?

I am looking to plot, in a barplot, the differences in value between two groups (Elevated Temp and Control).
I'd like to be able to plot these in the same way as my original graph with Months along the x axis.
Here is the following script I have used to get to the current barplot 1 that I have plotted. This shows y axis= plant growth and x axis=Months.
Script: Current Barplot
Tempmean<- data %>% group_by (Treatment, Month) %>% summarize (TTmean = mean(Amean, na.rm=TRUE), TTsd=sd(Amean,na.rm=TRUE))
p<-ggplot(data=Tempmean, aes(x=factor(Month), y=TTmean, fill=Treatment)) +
geom_bar(stat="identity", position="dodge", colour="black" , size = 0.25, width=0.5) + geom_errorbar(aes(ymin=TTmean-TTsd, ymax=TTmean+TTsd), width=.1,
position=position_dodge(.5)) + scale_fill_manual(values=c("darkgray","darkolivegreen")) + scale_x_discrete(breaks=6:8,labels=c("June","July","August")) + scale_y_continuous(limits=c(0,20), breaks=seq(0,20,2))
p
This is the data I am working with 2. I would be looking to take the TTmean of the eCO2 from the TTmean of the aCO2.
Data:
structure(list(Treatment = c("aCO2", "aCO2", "aCO2", "eCO2","eCO2", "eCO2"), Month = c(6L, 7L, 8L, 6L, 7L, 8L), TTmean = c(10.1922587348143,10.1061784054575, 8.27148533916994, 12.0261355594138,10.8954781586458, 10.9468200269188), TTsd =c(7.04936647397141,4.18653008350561, 1.50026716071241, 3.25471492346035, 0.742036555955107, 2.00464198948226)), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Treatment", drop = TRUE, indices = list(0:2, 3:5), group_sizes = c(3L, 3L), biggest_group_size = 3L, labels = structure(list(Treatment = c("aCO2", "eCO2")), row.names = c(NA, -2L),class = "data.frame", vars = "Treatment", drop = TRUE))
This should do the trick.
I dropped TTsd because it doesn't seem like you need it. The trick is to spread() the data so you can easily compute the difference in the values. I computed as aCO2 minus eCO2; but you can change that in mutate()
library(tidyverse)
Tempmean %>%
select(-TTsd) %>%
# either group_by Month, or just ungroup entirely
group_by(Month) %>%
spread(Treatment, TTmean) %>%
mutate(T_diff = aCO2 - eCO2) %>%
ggplot(aes(factor(Month), T_diff)) %+%
geom_bar(position = "dodge", stat = "identity", size = 0.25, width=0.5) %+%
scale_x_discrete(breaks=6:8,labels=c("June","July","August"))

Heatmaps for a matrix with ones and zeros using R

Below is my sample data, basically its a matrix with row names as person names
and some columns for each of these rows. All I have in the data is just zeros and ones. I would like to visualize it using heatmaps. (reds for 0s and green for 1s or any other color coding). How do I accomplish this using R? you can show me using any example dataset with just ones and zeros (binary values).
Just another approach using ggplot
library(ggplot2)
library(reshape2)
library(plyr)
library(scales)
df <- structure(list(people = structure(c(2L, 1L), .Label = c("Dwayne", "LeBron"), class = "factor"),
G = c(1L, 0L),
MIN = c(1L, 0L),
PTS = c(0L, 1L),
FGM = c(0L,0L),
FGA = c(0L,0L),
FGP = c(1L,1L)),
.Names = c("people", "G", "MIN", "PTS", "FGM", "FGA", "FGP"),
class = "data.frame",
row.names = c(NA, -2L))
df.m <- melt(df)
df1.m <- ddply(df.m, .(variable), transform, rescale = value)
p <- ggplot(df1.m, aes(variable, people)) +
geom_tile(aes(fill = rescale), colour = "black")
p + scale_fill_gradient(low = "green", high = "red")
show(p)
Adopted from this tutorial
With highcharter:
library(highcharter)
library(tidyr)
library(dplyr)
df<-data.frame(row=c("Dwayne","James"),G=c(1,0),MIN=c(1,0),PTS=c(0,1),FGM=c(0,0),FGA=c(0,0),FGP=c(1,1))
rownames(df)<-c("Dwayne","James")
df$row<-rownames(df)
data<-df%>%
tidyr::gather(row,value)%>%
setNames(c("name","variable","value"))
hchart(data, "heatmap", hcaes(x = variable, y = name, value = value)) %>%
hc_colorAxis(stops = color_stops(2, c("red","green")))
UPDATE:
You can add hc_size(height = 800) for height=800 or make something like that
x<-50
hg<-length(unique(data$name))*x+100
hchart(data, "heatmap", hcaes(x = variable, y = name, value = value)) %>%
hc_colorAxis(stops = color_stops(2, c("red","green")))%>%
hc_size(height = hg)
Where each row in dataset makes chart bigger by 50 points. You can change it in x
This answer uses plotly and hence adding it as another answer. Using the same data as the following one.
library(plotly)
df1 <- as.matrix(df)
p <- plot_ly(x = colnames(df), y = df[,1], z = as.matrix(df[-1]), colors = colorRamp(c("green", "red")), type = "heatmap")
This is much simpler than the ggplot2 in terms of getting the output.
Hope this helps!

Resources