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:
- Can I add some code expression into my lines to generate a legend element for each set of data? I thought this would be a command such as 'fill' or 'shape' - but I am wrong
- Is there a reason why I am not g generating a legend full stop? I was expecting at least a legend with one set of data - but it appears with none - is one deconflicting with another?
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]


