I am trying to combine multiple datasets into one figure in r. My code doesn't seem to be generating a legend for this data.
My two questions are:
If the above cannot be done, then is there a straightforward way to plot this figure using a 3 column data.table using the groupings of each data?
The code is below. You wont be able to reproduce it as I would need to send you the CSV files ( the datasets).
I have included the output figure from the code copied below. Idealy I would like a legend that lists a label for each different line/point +/- a text label at each line.
Thank you for any help!
library(tidyverse)
#These are the datasets I am turning into three dataframes #
bowen.data <- bowen
df <- data.frame(bowen)
lit.data <- lit_data
df <- data.frame(lit_data)
shock.data <- shock_tube_tests
df <- data.frame(shock_tube_tests)
high.data <- high_shock_data
df <- data.frame(high_shock_data)
low.data <- low_shock_data
df <- data.frame(low_shock_data)
#setting limit for my axis#
options(scipen = 1000000)
library(ggplot2)
#Now I wish to plot alll this data on one figure. This data describes duration and peak overpressure of shock waves. Each line of geom_line is trying to plot each datasset to produce each line, or points. I then set the X & Y limiitss, the labelss and the colours. #
ggplot(bowen.data, aes(x=Duration)) +
geom_line(aes(y = survival99), color = "cornflowerblue", linetype="longdash") + geom_line(aes(y = survival90), color="dodgerblue1", linetype="dashed") +
geom_line(aes(y = survival50), color="steelblue", linetype="solid") +
geom_line(aes(y = survival10), color="dodgerblue2", linetype="dotdash") +
geom_line(aes(y = survival1), color="dodgerblue3", linetype="twodash") + geom_line(aes(y = lung), color="darkslategrey", linetype="solid") +
ylim(100,1100000) + xlim(0.2,20) + ylab("Peak Overpressure") + xlab("Duration") +
geom_point(data=high.data, aes(x=duration, y=high), color='seagreen4') +
geom_point(data=low.data, aes(x=duration, y=low), color='indianred') +
geom_point(data=low.data, aes(x=duration, y=low), color='indianred', shape = 13) +
geom_point(data=shock.data, aes(x=duration, y=tube), color='violetred4', shape = 17) +
geom_point(data=lit.data, aes(x=dura, y=lit), color='orange')
Using dput:
structure(list(Duration = c(0.2, 0.3, 0.4, 0.5, 0.6), survival99 = c(3509982.865,
2422907.195, 1883026.274, 1555445.788, 1348277.839), survival90 = c(4138911.806,
2846984, 2206434.933, 1822870.548, 1566705.278), survival50 = c(5104973.144,
3490782.825, 2693285.691, 2217270.161, 1900462.526), survival10 = c(6313217.275,
4294375.461, 3299414.021, 2705203.586, 2313630.72), survival1 = c(7513231.158,
5090961.551, 3899360.722, 3190981.429, 2711178.007), lung = c(1020994.629,
698156.565, 538657.1381, 443454.0321, 380092.5051), X8 = c(NA,
NA, NA, NA, NA)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
[
shock data
structure(list(duration = c(1.00244911, 0.947052916, 1.675566344,
1.6586253, 1.837305476), tube = c(24973.80469, 28125.45703, 169033.3438,
165488.6719, 285638.9375)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
lit data
structure(list(dura = c(2, 6, 1.3, 6.9, 2), lit = c(1000000,
1000000, 760000, 760000, 450000)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
[ `enter image description here][1]
I don't see your desired output. The idea is to assign a colour to each line by mapping the colour aesthetic
to a constant string. The simplest option is to select the string that will appear in the legend. It is critical that this is done within the aes
call; you are creating a mapping to this variable.
These strings can now be mapped to the appropriate colours by scale color manual.
library(ggplot2)
library(tidyverse)
library(ggthemes)
ggplot(bowen.data, aes(x=Duration)) +
geom_line(aes(y = survival99, color="survival99"), linetype="longdash", size=2) +
geom_line(aes(y = survival90, color="survival90"), linetype="dashed", size=2) +
geom_line(aes(y = survival50, color="survival50"), linetype="solid", size=2) +
geom_line(aes(y = survival10, color="survival10"), linetype="dotdash", size=2) +
geom_line(aes(y = survival1, color="survival1"), linetype="twodash", size=2) +
geom_line(aes(y = lung, color="Lung"), linetype="solid", size=2) +
ylim(100,1100000) + xlim(0.2,20) + ylab("Peak Overpressure") + xlab("Duration") +
#geom_point(data=high.data, aes(x=duration, y=high), color='seagreen4') +
#geom_point(data=low.data, aes(x=duration, y=low), color='indianred') +
#geom_point(data=low.data, aes(x=duration, y=low), color='indianred', shape = 13) +
geom_point(data=shock.data, aes(x=duration, y=tube, color='shock.data'), shape = 17, size=3) +
geom_point(data=lit.data, aes(x=dura, y=lit, color='lit.data'), size=3)+
theme_pander()+
theme(axis.text.x = element_text( hjust = 1, face="bold", size=12, color="black"),
axis.title.x = element_text(face="bold", size=16, color="black"),
axis.text.y = element_text(face="bold", size=12, color="black"),
axis.title.y = element_text(face="bold", size=16, color="black"),
legend.title=element_blank(),
legend.text = element_text(family="Times", color = "black", size = 16,face="bold"))+
scale_color_manual(values = c("survival1"="dodgerblue3","survival10"="dodgerblue2", "survival50"="steelblue", "survival90"="dodgerblue1", "survival99"="cornflowerblue", "lit.data"="orange","Lung"="darkslategrey","shock.data"="violetred4"))
Plot:
Also you need to adjust the range of the ylim
and xlim
. Below ylim(0,6600000) + xlim(0.2, 8)
If you want to add labels to lines ( bowen.data
), I recommend using the using the directlabels
library.
Sample code:
library(dplyr)
library(ggplot2)
library(ggthemes)
library(directlabels)
bowen.data%>%
pivot_longer(cols = -1) %>%
ggplot(aes(x=Duration,y=value))+
geom_line(aes(linetype=name, color=name), size=2 )+
scale_color_manual(values = c("lung"="darkslategrey", "survival1"="dodgerblue3","survival10"="dodgerblue2", "survival50"="steelblue", "survival90"="dodgerblue1", "survival99"="cornflowerblue"))+
scale_linetype_manual(values=c("lung"="solid","survival1" = "twodash", "survival10" = "dotdash", "survival50" = "solid" , "survival90" = "dashed", "survival99" = "longdash"))+
geom_dl(aes(label = name), method = list(dl.trans(x = x + 0.2),"last.points", cex = 1.2, fontface='bold'))+
ylim(0,6600000) + xlim(0.2, 8) +
labs(x="Duration",y="Peak Overpressure", fill="Factors") +
theme_pander()+
theme(axis.text.x = element_text( hjust = 1, face="bold", size=12, color="black"),
axis.title.x = element_text(face="bold", size=16, color="black"),
axis.text.y = element_text(face="bold", size=12, color="black"),
axis.title.y = element_text(face="bold", size=16, color="black"),
legend.title=element_blank(),
legend.text = element_text(family="Times", color = "black", size = 16,face="bold"))
Plot:
Sample data:
bowen.data <-
structure(
list(
Duration = c(0.2, 0.3, 0.4, 0.5, 0.6),
survival99 = c(
3509982.865,
2422907.195,
1883026.274,
1555445.788,
1348277.839
),
survival90 = c(4138911.806,
2846984, 2206434.933, 1822870.548, 1566705.278),
survival50 = c(
5104973.144,
3490782.825,
2693285.691,
2217270.161,
1900462.526
),
survival10 = c(6313217.275,
4294375.461, 3299414.021, 2705203.586, 2313630.72),
survival1 = c(
7513231.158,
5090961.551,
3899360.722,
3190981.429,
2711178.007
),
lung = c(1020994.629,
698156.565, 538657.1381, 443454.0321, 380092.5051),
X8 = c(NA,
NA, NA, NA, NA)
),
row.names = c(NA,-5L),
class = c("tbl_df",
"tbl", "data.frame")
)
shock.data<-structure(
list(
duration = c(1.00244911, 0.947052916, 1.675566344,
1.6586253, 1.837305476),
tube = c(
24973.80469,
28125.45703,
169033.3438,
165488.6719,
285638.9375
)
),
row.names = c(NA,-5L),
class = c("tbl_df",
"tbl", "data.frame")
)
lit.data<-structure(
list(
dura = c(2, 6, 1.3, 6.9, 2),
lit = c(1000000,
1000000, 760000, 760000, 450000)
),
row.names = c(NA,-5L),
class = c("tbl_df",
"tbl", "data.frame")
)
#These are the datasets I am turning into three dataframes #
bowen.data <- bowen
df <- data.frame(bowen)
lit.data <- lit_data
df <- data.frame(lit_data)
shock.data <- shock_tube_tests
df <- data.frame(shock_tube_tests)
#high.data <- high_shock_data
#df <- data.frame(high_shock_data)
#low.data <- low_shock_data
#df <- data.frame(low_shock_data)
#setting limit for my axis#
#options(scipen = 1000000)
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.