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"))
4
  • ggplot(q12, aes(x = V1, y = V2) + geom_point()? Commented Jan 11, 2017 at 19:05
  • @NathanDay i've ten columns, from V1 to V10. I'll add that. Commented Jan 11, 2017 at 19:14
  • I am not clear about what column(s) go on the x-axis and y-axis. Do you want a pairs plot like this? Commented Jan 11, 2017 at 19:19
  • @Gregor maybe this image helps to clarify that. Commented Jan 11, 2017 at 19:32

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

3 Comments

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!
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.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.