0

My data frame is like this:

slf<-read.csv("slf_sma.csv", header = TRUE)

 Old    Middle     Young         O         M         Y
1  0.5565666 0.5993509 0.6419396  0.016880  0.016020  0.009166
2  0.5867939 0.5915410 0.6009692  0.066140  0.124500 -0.008612
3  0.6131070 0.6212041 0.5977287  0.064710  0.043830  0.046360
4  0.5924194 0.5934464 0.6058512  0.064490 -0.022000  0.038500
5  0.5863441 0.6216458 0.6151412  0.006078  0.027570  0.043890
6  0.6070884 0.6115213 0.6646005  0.027620 -0.036370  0.051400
7  0.5951457 0.5615895 0.6170768  0.052400 -0.017770  0.028030
8  0.5915017 0.6080891 0.6124820  0.076530  0.033260 -0.008699
9  0.5872558 0.5839422 0.6272849  0.016040 -0.020230  0.006001
10 0.5839430 0.6146755 0.6274087  0.003102 -0.037940  0.011850
11 0.6035122 0.5993125 0.6092055  0.005152  0.020510 -0.083200
12 0.5821265 0.5919774 0.6068244 -0.016630  0.092710  0.004364
13 0.5709662 0.6023472 0.6089293 -0.058840 -0.097890  0.067540
14 0.5703071 0.6286741 0.6231253  0.014740 -0.008209  0.029280
15 0.5794983 0.5959640 0.6061620 -0.012460  0.101500  0.061760
16 0.5705437 0.6049343 0.6053916  0.076610  0.057100  0.053070
17 0.6136727 0.5996563 0.6400071  0.087110 -0.010240  0.044100
18 0.5706490 0.5872884 0.5984836  0.051490  0.012680  0.120700
19 0.5806012 0.5860316 0.6250796  0.042060  0.151200  0.071570
20 0.5761660 0.5827755 0.6209389  0.036160 -0.018770 -0.041920
21 0.5795988        NA 0.6157653  0.103900        NA -0.028060
22 0.6047752        NA 0.6064769  0.021110        NA  0.094800
23 0.5553546        NA 0.6077610 -0.022240        NA -0.022890
24 0.5808579        NA 0.6336737 -0.001092        NA -0.001328
25        NA        NA 0.6415534        NA        NA  0.043790
26        NA        NA 0.5883105        NA        NA  0.047710
27        NA        NA 0.6266886        NA        NA -0.024540

I want to plot using ggplot2. First I used x<-melt(slf$Old, slf$Middle, slf$Young) to use the values in y axis and the other O, M, Y on the x axis.

x<-melt(slf$Old, slf$Middle, slf$Young) 

The relationship between Young, Y was negative before the melting and merging them. Using the command:

ggplot(slf1, aes(slf1$x, y = value, color = variable)) + geom_smooth(method="lm", alpha = 0.3, color = "green", fill="coral2", size = 2, aes(y = y1, col = "Old")) + geom_smooth(method="lm", color = "red", fill = "maroon", alpha = 0.3, size = 2, aes(y = y2, col = "Middle")) + geom_smooth(method="lm", color = "yellow", fill = "seagreen4", alpha = 0.3, size = 2, aes(y = y3, col = "Young")) + theme_classic()

the plot appeared to be positive. I don't know what went wrong. Any help is appreciated.

enter image description here

  • Your question is unclear, please read and edit your question according to: [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). What's `slf1`? – pogibas Aug 26 '18 at 08:10
  • @PoGibas, thank you. slf is the name of the data before melting and slf1 is after melt. I edited it again. Thank you –  Aug 26 '18 at 08:21

1 Answers1

0

It's the way you prepare your data. I would recommend subsetting full names and IDs and melting them separately.

# Melt and ggplot2
library(tidyverse)

# Subset full names; subset IDs
# Melt individually and cbind
plotData <- cbind(melt(d[, 1:3]), melt(d[, -c(1:3)]))
colnames(plotData) <- c("var1", "val1", "var2", "val2")

# How data looks like before plotting
#  var1      val1 var2     val2
#1  Old 0.5565666    O 0.016880
#2  Old 0.5867939    O 0.066140
#3  Old 0.6131070    O 0.064710
#4  Old 0.5924194    O 0.064490
#5  Old 0.5863441    O 0.006078

# Plot with single geom_smooth
ggplot(plotData, aes(val1, val2, color = var1)) +
    geom_smooth(method = "lm")

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • Thank you, @PoGibas. I tried your command and it says 'Error in d[, 1:3] : incorrect number of dimensions' –  Aug 26 '18 at 08:54
  • @AbiotYenealem Have you adjusted code to your data? Change object name (I don't know if it's `slf` or `slf1`). Also, does it have same number of dimensions? Also, make sure it's a data frame with `d <- as.data.fame(DATA)` – pogibas Aug 26 '18 at 08:56
  • I am new and I barely understand what the command `plotData <- cbind(melt(d[, 1:3]), melt(d[, -c(1:3)]))` related to the original data `slf`. Is `d' the name of the object, in my case slf? –  Aug 26 '18 at 09:27
  • @AbiotYenealem `d` is the name of the object that has 6 columns (Old, Middle, Young, O, M, Y) – pogibas Aug 26 '18 at 09:28