1

I'm working in R and i've a problem :(. I've this dataframe that i want to plot using ggplot:

          V1         V2         V3          V4         V5         V6         V7         V8         V9        V10
1 -0.8067465 -0.4170181  0.9380890 0.613084937  0.4583916  0.4175951 -0.7183698 -0.3479931 -0.3392734 -0.8516039
2 -0.2893608 -0.8066911  0.1534129 0.008801641 -0.1916244 -0.6729157  0.6154498  0.1914066  0.3660398  0.9019622
3 -0.9536465  0.9645535 -0.6915938 0.328425519 -0.4197832 -0.2064765 -0.4328127  0.7849686 -0.2665250  0.6206650

I need to plot each column together to another one, just like facet_grid does it. In the X-axis i only need, for each plot, adjust the limits based in an intervale (e.g.:[-1,1]), but not a specific column/variable. The Y-axis works fine.

My principal problem is that i didn't know how to generate each plot using column name and their three points (or n points) in the same graph for every column in a dataframe. I tried some like this:

ggplot(q12, aes(x = V1, y = row.names(q12))) + 
  geom_point()+
    facet_grid(.~ V1)

that generate this plot and i want some like this.

I know that V1 has to be the list of columns, but i have no idea how to do. Use colnames(q12) doesn't works, and the i need the three points in the same graph.

In my previous experience using ggplot i had a column with an ID o an attribute for plotting. This case is a little different, i research a lot to do it, but i didn't find any solution. I tried with melt without results, maybe i didn't know how it works.

If someone could help me step by step, for learn it, i would be very grateful.

Data

structure(list(V1 = c(-0.8067465, -0.2893608, -0.9536465), V2 = c(-0.4170181, 
-0.8066911, 0.9645535), V3 = c(0.938089, 0.1534129, -0.6915938
), V4 = c(0.613084937, 0.008801641, 0.328425519), V5 = c(0.4583916, 
-0.1916244, -0.4197832), V6 = c(0.4175951, -0.6729157, -0.2064765
), V7 = c(-0.7183698, 0.6154498, -0.4328127), V8 = c(-0.3479931, 
0.1914066, 0.7849686), V9 = c(-0.3392734, 0.3660398, -0.266525
), V10 = c(-0.8516039, 0.9019622, 0.620665)), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10"), class = "data.frame", row.names = c("1", 
"2", "3"))
Community
  • 1
  • 1
Felipe Piña
  • 89
  • 1
  • 2
  • 11

1 Answers1

1

You need to do two things:

  1. Add the column for your y values (row names are a bad place for meaningful information)
  2. Convert your data into a long format.

Here we go:

# Add ID column:
q12$id = 1:nrow(q12)
# maybe better for you: q12$id = row.names(q12)

# Change data to long format
## original answer 
library(reshape2)
df_long = melt(q12, id.vars = "id")
## more modern approach: use `tidyr::pivot_longer`


# plot data
ggplot(df_long, aes(x = value, y = id)) + 
    geom_point() +
    facet_wrap(~ variable)

I used this data nicely produced with dput():

q12 = structure(list(V1 = c(-0.8067465, -0.2893608, -0.9536465), V2 = c(-0.4170181, 
-0.8066911, 0.9645535), V3 = c(0.938089, 0.1534129, -0.6915938
), V4 = c(0.613084937, 0.008801641, 0.328425519), V5 = c(0.4583916, 
-0.1916244, -0.4197832), V6 = c(0.4175951, -0.6729157, -0.2064765
), V7 = c(-0.7183698, 0.6154498, -0.4328127), V8 = c(-0.3479931, 
0.1914066, 0.7849686), V9 = c(-0.3392734, 0.3660398, -0.266525
), V10 = c(-0.8516039, 0.9019622, 0.620665)), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10"), row.names = c("1", 
"2", "3"), class = "data.frame")
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks! That's i wanted. Very clear your method. Finally, do you have a link to a website to see how to add a line that connect the points in each graph? Now i'll look for that, but maybe you know an existing topic for that. Thanks again! – Felipe Piña Jan 11 '17 at 20:02
  • Just add `+ geom_line()` to the plot definition. I'd recommend finding an introduction to ggplot, there are many good blogs and other resource out there. – Gregor Thomas Jan 11 '17 at 20:21
  • Yes, `geom_line()` and `group` can do that, but i need the line in order by "id" not for the x-asis, like default ggplot shows. Anyway, thanks for your time! **Edit:** `geom_path` do that! – Felipe Piña Jan 11 '17 at 20:34