0

I want to do a line plot with R similar to the way Excel is plotting it:

See picture here:

Can somebody help me how I can do this with ggplot?

This is the code to have the same dataset as in the picture:

id = c(1,2,3,4)
Na = c(600,1100,2500,400)
Ca = c(53,106,932,8)
Mg = c(18,32,975,2)
Cl = c(463,1960,13330,590)
HCO3 = c(698,165,189,640)
SO4 = c(25,62,1068,2)
unit = c("Series 1", "Series 2","Series 3","Series 4" )
data = data.frame(cbind(id,Na,Ca,Mg,Cl,HCO3,SO4,unit))
data

I know that I can fill graphs with ggplot with a factor. I have a bigger data set with those 4 series. Each ID is assigned to one of the series, so this are just the averages. At the end I want to do a plot of several lines (not just this 4) and colour the lines regarding their unit (Series).

It would be great if someone even can give me a hint how I can plot this with the different variables on the x-axis and their value [mg/L] on the y-axis. I prefer the y-axis being in logarithmic scale, if possible.

Thanks in advance for helping a R-Beginner in ggplot2!

A_beginner
  • 69
  • 4
  • Your question is in essence a duplicate of the linked question; the extension to more than 2 plots is straightforward. It boils down to reshaping your data from wide to long, and then use the `colour` aesthetic to map different series to different colours. Please let us know if this does *not* answer your question. – Maurits Evers Jul 24 '18 at 02:25
  • The following should get you started: `library(tidyverse); data %>% gather(what, val, -id, -unit) %>% ggplot(aes(x = what, y = val, colour = unit, group = unit)) + geom_line()` – Maurits Evers Jul 24 '18 at 02:28
  • Hi, thanks for your answer. I had a look at the question but it didn't really help. The plot I get is not correct. The y-axis is not arranged descending. Do I have to change the type of the variables somehow? How can I plot them in a logarithmic scale? – A_beginner Jul 24 '18 at 04:52
  • I there is a misunderstanding about the purpose of this site. SO is not a tutorial service, nor should you come and post here if you haven't done any research yourself. **Plotting in `ggplot`, reordering factors and scale transformations are *extremely* common topics**: you can find plenty of tutorials on the net and many questions on SO deal with those issues and have great answers. SO is about specific coding questions: meaning you have a code attempt, and only if you haven't found help in any existing SO Q&As should you post your question here. Otherwise this becomes just another dupe. – Maurits Evers Jul 24 '18 at 06:01
  • I am totally aware of it and I have done a lot of searching in the net concerning ggplot. Most of my issues I was able to solve but I still can't solve the Schöller-Plot Problem. There is nothing I can find about this on the net either. That is why I posted the question. I am sure there are more people out there which like to plot their chemical analysis this way. Maybe one of them has an answer or at least other people are interested in an answer as well. I know that parts of my question might be common issues but so far I'm not able to combine these codes to really get a proper Schöller Plot – A_beginner Jul 27 '18 at 00:45

0 Answers0