Reputation: 31
I'm trying to figure out the best way to plot several depth profiles with Depth on the y-axis (obviously) and Salinity and Temperature on the x-axis.
It works fine with just Temperature, faceting the graphs by date. But I want to include Salinity in my profiles. Problem is, as temperature values are between 0 and 2°C and Salinity is always >34 I would have to use different scales. I can't figure out a good way to plot both variables over depth for several dates.
I would really appreciate help in this case. The code I used for plotting my profiles without Salinity is this:
scatter <- ggplot(profile, aes(Temp, Depth, colour = Date))
scatter + geom_point(size = 1.5) +
labs(x = "Temperature [°C]", y = "Depth [m]", colour = "Date") +
facet_wrap(~ Date,ncol=5, scales="fixed") +
scale_y_reverse() +
scale_color_discrete(guide="none") +
theme(plot.title = element_text(size = 18, vjust = 1)) +
theme(axis.text.x = element_text(size = 5, angle = 0, colour = 1)) +
theme(axis.text.y = element_text(size = 14, colour = 1)) +
theme(axis.title.x = element_text(size = 18, vjust = -0.5)) +
theme(axis.title.y = element_text(size = 18, angle = 90, vjust = 0.3)) +
theme(axis.line = element_line(size = 0)) +
theme(axis.ticks = element_line(size = 0.1))
If there is no way to do it with ggplot? Other possible solutions?
Upvotes: 3
Views: 7372
Reputation: 41
edit: I realize this is not a solution to your problem. However, your code did help me solve mine. therefore I will look into how to balloon this out to faceting. You should also include your data.
I have a solution for this problem. I've been working on depth profiles for a class. I've found that you can mess up super bad if you are not careful. I've narrowed this down to one graphic for the sake of my example. I will also include my dataframe.
Dataframe
date station depth abs conc
1 9/28/2019 S4.5 0 0.049 36.69231
2 9/28/2019 S4.5 10 0.049 36.69231
3 9/28/2019 S4.5 20 0.054 40.53846
4 9/28/2019 S4.5 30 0.054 40.53846
5 9/28/2019 S4.5 40 0.056 42.07692
6 9/28/2019 S4.5 50 0.062 46.69231
7 9/28/2019 S4.5 75 0.065 49.00000
8 9/28/2019 S4.5 100 0.085 64.38462
9 9/28/2019 S4.5 125 0.094 71.30769
10 9/28/2019 S4.5 150 0.089 67.46154
Please note the headings "date","station","depth","abs","conc"
#treat conc as a function of depth and then reverse later
ggplot(data,aes(x=depth,y=conc))+
#set up the asthetics, the line must be broken because this is technically discrete data
geom_line(color="red",size=1, linetype=2)+
geom_point()+
#reverse depth so it starts at zero
scale_x_reverse()+
#put the y axis labes on the opposite side so when its flipped it will appear at top
scale_y_continuous(position="right")+
#this is how you reverse the look and order or the coordinates for the graph
coord_flip()
I found that by simply reversing the y scale you have have behaviour that transforms how the data is projected if you are doing other transformations. Some additions that are required according to my class is having the value at the top start at 0. To do this you will need to set a limit for the "x" axis. This is done by adding arguments to coordinate flip.
...
#The order of the y lim matters! the vector must be ordered first value last value
coord_flip(ylim = c(<yourfirstvalue>,<yourlastvalue>))
...
As far as I can tell other transformations do not project the data in a way that makes sense to my markers. If others have a better description of a solution to this problem please feel free to correct me.
Upvotes: 3