I want to build a ggplot graph for a given data.frame with one x-axis and multiple y.curves. Also, I want to do it within a customized function so i could call this function anytime I want to plot something with various dataframes.
The script I'm trying to develop is:
graph.date <- function(data, y.axis1, y.axis2, y.axis3, y.axis4, y.axis5, y.axis6, y.axis7, x.axis, y.lab, title, ...){
ggplot(data, aes_string(x = x.axis)) +
ylab(label = y.lab) + xlab(label = "Date") +
ggtitle(label = title) +
scale_x_date(breaks = "1 month", labels = date_format("%d-%b-%Y")) +
geom_line(aes(y = y.axis1, colour = y.axis1), size = 1) +
geom_line(aes(y = y.axis2, colour = y.axis2), size = 1) +
geom_line(aes(y = y.axis3, colour = y.axis3), size = 1) +
geom_line(aes(y = y.axis4, colour = y.axis4), size = 1) +
geom_line(aes(y = y.axis5, colour = y.axis5), size = 1) +
geom_line(aes(y = y.axis6, colour = y.axis6), size = 1) +
geom_line(aes(y = y.axis7, colour = y.axis7), size = 1) +
scale_fill_discrete() + scale_color_manual(values = c(brewer.pal(9, "Set1"), brewer.pal(9, "Set1"))) +
labs(colour = "") + theme(plot.title = element_text(size = rel(1.76))) +
guides(colour = guide_legend(override.aes = list(size=3))) +
theme(text = element_text(size=20), axis.title=element_text(size=34,face="bold"), axis.text.x = element_text(face="bold",
color="black", size=24, angle=25), axis.text.y = element_text(face="bold", color="black", size=24, angle=0))
}
Then I am calling the function:
graph.date(data = BelgiumMerged, y.axis1 = "Gen1", y.axis2 = "Gen2", y.axis3 = "Gen3",
x.axis = "Date", y.lab = "Capacity", title = "title")
The error I get is :
Error in eval(expr, envir, enclos) : object 'y.axis1' not found
The error you get is that df does not have a column called y.axis1. The easiest way to refer to the column that have the name that is stored in the variable y.axis1 is to use aes_string() instead of aes(). Also don't set the color in the call to aes()
So change all
geom_line(aes(y = y.axis1, colour = y.axis1), size = 1)
to
geom_line(aes_string(y = y.axis1), size = 1,color="red") # Or whatever color you want
However a better way to solve the problem is to reshape the dataframe to long formating so that all x coordinates lays in one column all y coordinates in one column and the grouping of these in a third column. Your function could then be defined as
graph.date <- function(df,y.axes,x.axis){
index <- which(names(df) %in% y.axes)
plotDF <- gather(df,y.type,y.data,index)
ggplot(plotDF,aes_string(x.axis)) +
geom_line(mapping=aes(y=y.data,color=y.type))
}
Here you will pass a vector of y axes instead of having one parameter for each y axis
Thanks a lot Nist - you're a RockStar
My final script looks as follows:
graph.date <- function(data,y.axes,x.axis, y.lab, x.lab, title){
index <- which(names(data) %in% y.axes)
plotDF <- gather(data,y.type,y.data,index)
ggplot(plotDF,aes_string(x.axis)) + ggtitle(label = title) + ylab("Capacity [MW]") + xlab("Date") +
geom_line(mapping=aes(y=y.data,color=y.type))+
scale_fill_discrete() + scale_x_date(breaks = "1 month", labels = date_format("%d-%b-%Y")) +
scale_color_manual(values = c(brewer.pal(9, "Set1"), brewer.pal(9, "Set1"))) +
labs(colour = "LegendTitle") + theme(plot.title = element_text(size = rel(1.76))) +
guides(colour = guide_legend(override.aes = list(size=3))) +
theme(text = element_text(size=20), axis.title=element_text(size=34,face="bold"),
axis.text.x = element_text(face="bold", color="black", size=24, angle=25),
axis.text.y = element_text(face="bold", color="black", size=24, angle=0))
}
#Calling the function
graph.date(df, y.axes = c("Gen1", "Gen2", "Gen3"), x.axis = "Date", title = "title")
Related
I would like to know how to use ggplot2 to create a side by side plots with one common legend. I have seen some similar questions but not sure how to directly apply it to my code. I have provided my code for the graphs with the legend and some data that can be used to recreate the graphs.
Stocks1<-c(2,1,0.8,0.7,0.6)
Bonds1<-c(1,0.8,0.7,0.6,0.5)
Cash1<-1-(Stocks1+Bonds1)
Stocks2<-c(0.6,0.5,0.4,0.3,0.2)
Bonds2<-c(0.3,0.2,0.2,0.15,0.1)
Cash2<-1-(Stocks2+Bonds2)
H<-length(Stocks1) #Change value to represent data
t <- seq(from = 0, to = H, 1) # time grid
And here are the two graphs
pi_F<- data.frame(cash = Cash1, bonds = Bonds1,
stocks= Stocks1,time=t[-1])
melted_F <- melt(pi_F, id.vars = 'time')
ggplot(melted_F, aes(x=time, y=value, group = variable)) +
geom_area(aes(fill=variable)) +
scale_fill_manual(values=c("#2E318F", "#DFAE41","#109FC6"),
name="Asset Type",
labels = c("Bank account","Bonds", "Stocks"))+
scale_x_continuous(name = 'Age',
breaks = seq(1,H,1)) +
scale_y_continuous(name = 'Asset allocation (in %)',
labels=scales::percent,
breaks = seq(0,1,0.1),
sec.axis = sec_axis(~.*1,breaks = seq(0,1,0.1),labels=scales::percent)) +
coord_cartesian(xlim = c(1,H), ylim = c(0,1), expand = TRUE) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
pi_F<- data.frame(cash = Cash2, bonds = Bonds2,
stocks= Stocks2,time=t[-1])
melted_F <- melt(pi_F, id.vars = 'time')
ggplot(melted_F, aes(x=time, y=value, group = variable)) +
geom_area(aes(fill=variable)) +
scale_fill_manual(values=c("#2E318F", "#DFAE41","#109FC6"),
name="Asset Type",
labels = c("Bank account","Bonds", "Stocks"))+
scale_x_continuous(name = 'Age',
breaks = seq(1,H,1)) +
scale_y_continuous(name = 'Asset allocation (in %)',
labels=scales::percent,
breaks = seq(0,1,0.1),
sec.axis = sec_axis(~.*1,breaks = seq(0,1,0.1),labels=scales::percent)) +
coord_cartesian(xlim = c(1,H), ylim = c(0,1), expand = TRUE) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
Idealy I would like these side by side with the legend in an appropriate place, probably to the right of both graphs. Thanks in advance for the help!
Put your data together and use facets:
## calling the first data `melted_F` and the second `melted_F2`
## put them in one data frame with a column named "data" to tell
## which is which
melted = dplyr::bind_rows(list(data1 = melted_F, data2 = melted_F2), .id = "data")
## exact same plot code until the last line
ggplot(melted, aes(x=time, y=value, group = variable)) +
geom_area(aes(fill=variable)) +
scale_fill_manual(values=c("#2E318F", "#DFAE41","#109FC6"),
name="Asset Type",
labels = c("Bank account","Bonds", "Stocks"))+
scale_x_continuous(name = 'Age',
breaks = seq(1,H,1)) +
scale_y_continuous(name = 'Asset allocation (in %)',
labels=scales::percent,
breaks = seq(0,1,0.1),
sec.axis = sec_axis(~.*1,breaks = seq(0,1,0.1),labels=scales::percent)) +
coord_cartesian(xlim = c(1,H), ylim = c(0,1), expand = TRUE) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
## facet by the column that identifies the data source
facet_wrap(~ data)
I wrote some code to make a graph (both below)
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) +
stat_summary(fun = "mean", geom = "bar", position = "dodge") +
theme_classic() +
scale_fill_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
scale_color_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
labs(x = 'Condition', y = 'Conflict (AUC)') +
scale_x_discrete(labels = c('Animal','Smoking')) +
coord_cartesian(ylim=c(0,1.5)) +
scale_y_continuous(expand = c(0,0))
p +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))
However, I recently read about 'ggpattern' and wondered if anyone could help me add some diagonal black lines to the yellow bars in my plot (e.g. ex-smokers conflict). I have tried multiple ways, but adding 'geom_col_pattern' to the code seems to mess up the Y axis and provide overall conflict for each condition (animal, smoking) rather than separately for smokers and ex-smokers. I think the 'geom_col_pattern' perhaps is not compatible with the 'stat_summary' I have in my code. Does anyone have any suggestions?
Thank you
Instead of adding a geom_col_pattern on top of your plot, just update the geom argument of stat_summary.
#replicate of your dataframe
for_plots <- data.frame(matrix(nrow = 100, ncol=0))
for_plots$condition <- sample(rep(c("Animal", "Smoking"), 100), 100)
for_plots$smoking_status <- sample(rep(c("Smokers", "Ex"), 100), 100)
n_smoking <- length(which(for_plots$condition == "Smoking"))
for_plots$conflict[for_plots$condition=="Smoking"] <- sample(seq(0.8, 1.3, length.out = n_smoking), n_smoking)
n_animal <- length(which(for_plots$condition == "Animal"))
for_plots$conflict[for_plots$condition=="Animal"] <- sample(seq(0.5, 1, length.out = n_animal), n_animal)
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) +
stat_summary(aes(pattern=smoking_status),
fun = "mean", position = "dodge",
geom = "bar_pattern", pattern_fill="black", colour="black") + #edited part
theme_classic() +
scale_fill_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
scale_color_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
labs(x = 'Condition', y = 'Conflict (AUC)') +
scale_pattern_manual(values=c("none", "stripe"))+ #edited part
scale_x_discrete(labels = c('Animal','Smoking')) +
coord_cartesian(ylim=c(0,1.5)) +
scale_y_continuous(expand = c(0,0))
p +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))
I cannot seem to change the legend title without the legend splitting my shape and color into two separate legends. How can i change the combined legend title? The image is what the graph looks like.
ggplot(data = df, aes (x = factor(dminp,c("-3 to -1", "-1 to 1")), y = sum_diff,col = factor(dmin), shape = factor(dmin), group = factor(dmin)))+
xlab("Range of Difficulty Parameters for Screen Items") + ylab("Bias Due to Skip-Logic") +
stat_summary(geom = "point",fun.y = "mean",size = 8, aes(shape = factor(dmin)))+
stat_summary(geom = "point",fun.y = "mean",size = 8, aes(col = factor(dmin)))+
scale_shape_manual(values = c(8,5)) + theme_bw() + scale_colour_manual(values = c("orange","purple"))+
theme(panel.grid.major.x = element_blank(),
panel.grid.major = element_line(colour = "black",size=0.25))+ theme(legend.justification = "top")
I have tried using labs(col = "what i want it to be named") but this adds a 2nd legend and splits shape/color.
How about trying:
... +
scale_shape_manual(name="X",values = c(8,5)) +
scale_colour_manual(name="X",values = c("orange","purple"))+
..
Here's an example:
ggplot(iris,aes(x=Sepal.Width,y=Sepal.Length,shape=Species,col=Species)) +
geom_point()+
scale_color_manual(name="X",values=c("Blue","Orange","Red")) +
scale_shape_manual(name="X",values=c(17,18,19))
I'm trying to plot a two "y" axis, the first one with "soil water content" in % (geom_line() ) and a geom_bar() with precipitation data. The problem is the precipitation chart. I need to "reverse" the plot.
I have this chart now:
and I need the soil water content time series as image above but precipitation as following image
Transforming the data do not solve the problem since i can not transform the bar plot for visualize it form upside down since the data are bars not points. Overmore when i reverse the plot both axis turn reverse
the "hum_melt10" data frame is a data frame with 3 columns: fecha = date (daily), value = water content (%) and variable = if the data is from a probe o from a model
the "pp_melt" data frame is a data frame with 3 columns: fecha = date (daily), value = cm of precipitation for each day and variable = if the water is from precipitation or irrigation
gpp = ggplot() +
geom_line(data = hum_melt10,aes(x = fecha, y = value, color = variable), size = 1.0) +
xlab("Fecha") +
geom_bar(data = pp_melt, aes(x = fecha, y = value / 20, fill = variable), stat="identity",position = 'dodge', na.rm = TRUE) +
scale_y_continuous(name = "Contenido de agua (%)",sec.axis = sec_axis(~.*20, name = "pp y riego (cm)")) +
scale_x_date(breaks = '2 month', labels = fecha, date_labels = '%b %y') +
theme(plot.title = element_text(lineheight=.8, face="bold", size = 20)) +
theme_bw() + theme( panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), aspect.ratio = 0.3)
Thank you!
Agreeing with #dmp, the use of sec_axis only adds the labels to the right; if you want to flip how it looks in the plot, you need to either
Use scale_y_reverse(), which will flip everything;
Manually flip the series in the aesthetic; or
Manually flip the series in the data itself.
Since you only want to reverse one of the data series and not all of them, #1 is out. #3 does work, though you'll still need to modify the call to sec_axis, so I'll keep it simple with doing just #2.
library(ggplot2)
mt <- transform(mtcars, rn = 1:nrow(mtcars))
ggplot(mt) +
geom_bar(aes(x = rn, y = drat), stat = "identity") +
geom_line(aes(x = rn, y = disp/100), stat = "identity", color = "red", size = 1) +
scale_y_continuous(sec.axis = sec_axis(~ . * 100))
Flipping just the red line, we change both how it is defined in the aesthetic (though it could be changed in the frame itself) and sec_axis.
ggplot(mt) +
geom_bar(aes(x = rn, y = drat), stat = "identity") +
geom_line(aes(x = rn, y = 5 - disp/100), stat = "identity", color = "red", size = 1) +
# changes: ^^^ vvv
scale_y_continuous(sec.axis = sec_axis(~ (5 - .) * 100))
(It's important to remember that the flipping point (5 in the example above) is based on the main-axis scale, not the new data.)
Without testing, I suspect the fix for your code would be something like this (where 9 is inferred from the plot).
gpp = ggplot() +
geom_line(data = hum_melt10,aes(x = fecha, y = value, color = variable), size = 1.0) +
xlab("Fecha") +
geom_bar(data = pp_melt, aes(x = fecha, y = 9 - value / 20, fill = variable), stat="identity",position = 'dodge', na.rm = TRUE) +
# changes: ^^^ vvv
scale_y_continuous(name = "Contenido de agua (%)", sec.axis = sec_axis(~(9 - .)*20, name = "pp y riego (cm)")) +
scale_x_date(breaks = '2 month', labels = fecha, date_labels = '%b %y') +
theme(plot.title = element_text(lineheight=.8, face="bold", size = 20)) +
theme_bw() + theme( panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), aspect.ratio = 0.3)
Still getting to grips with ggplot. My question: How do I manually change the line size? I've tried with scale_size_manual but it didn't seem to work.
setup:
test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")
ggplot(test.mat, aes(x=Window, y=value)) +
geom_line(aes(colour = type, linetype = type)) +
theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
specify size inside aes() function as follows:
ggplot(test.mat, aes(x=Window, y=value)) +
geom_line(aes(colour = type, linetype = type, size = type)) +
theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
Just turning #NelsonGon comment into an answer.
Is this what you want?
test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")
# -------------------------------------------------------------------------
base <- ggplot(test.mat, aes(x=Window, y=value))
#Here is where you need to add size
line_size <- base + geom_line(aes(colour = type, linetype = type), size=3)
line_size + theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
output
Update
If you want variable thickness for the individual lines, you can do as follows.
base <- ggplot(test.mat, aes(x=Window, y=value))
#Use an ifelse to add variable thickness
line_size <- base + geom_line(aes(colour = type, size=ifelse(type=="Cohort Alpha",1,2)))
line_size + guides(size = FALSE)
To follow up on my comment on deepseefan's answer
base +
geom_line(aes(colour = type,
size=factor(ifelse(type=="Cohort Alpha", "thick", "thin"),
levels=c("thick","thin")))) +
scale_colour_manual(values = c("black", "steelblue")) +
scale_size_manual(values = c(5, 1.4), guide = FALSE)