2

I'm trying to produce a scatter plot of laboratory mesurments stored in a data frame.

The data I use looks like this:

> print(df)
   day         val1         val2         val3
1    1      9.26875     11.34072     11.26673
2    2     48.47862     48.35817     48.68534
3    3    140.17682    141.09102    142.95175
4    4    313.43012    313.69816    312.97293

Each row shows which measurement were done for each day.

I would like to produce a scatter plot that would show just that.
The final result would look something like this.

I've found countless exemples about how to make a scatter plot treating each column as a different object, which is not what I want to do.

The closest thing I could find was this question, but the explaination is not clear, and I don't understand why the packages reshape2, and ggplot2 are required for this task.

Can anyone please give me any directions about that?

Best regards.

John C. Dough
  • 41
  • 1
  • 5
  • See http://ggplot2.tidyverse.org/reference/geom_point.html, past example one, and see if you can melt your dataframe down so your val1, val2, val3 are like the cylinders factor in the example. – mightypile Apr 25 '18 at 14:49
  • Linked post has many solutions. We need `reshape2` to reshape from wide-to-long format, then use ggplot to plot. There is also `matplot` function, look through all the answers, not all require ggplot. – zx8754 Apr 26 '18 at 06:30

1 Answers1

0

While not required, libraries give certain functionality that is not necessarily present or easy to find in base R. For example you can do a simple scatter plot with your dataframe:

df$day <- as.factor(df$day)
df <- melt(df)
plot(as.numeric(df$day),df$value)

boring plot

As you can see this generic plot can be achieved with a melt to get your data into long format with 1 column for your factors and 1 column for your values. However with a few lines of ggplot you can create a simple graph that is arguably more informative with only a little extra effort.

library(ggplot2)
ggplot(df, aes(x=day,y=value, colour = variable)) + geom_point(position = position_dodge(width = .3))

ggplot

You can learn more about ggplot2 here

jasbner
  • 2,253
  • 12
  • 24
  • Thank you. It's very short indeed! I'll need time to wrap my head around this. This isn't easy to understand for a newcomer like myself. – John C. Dough Apr 25 '18 at 14:58
  • Good luck. It can be overwhelming at first, so it's not a bad idea to start with the base functions. – jasbner Apr 25 '18 at 15:03