Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a third variable against x and y axis

Tags:

r

ggplot2

I am very new to R and I am trying to plot a third variable to a plot using ggplot2. I have searched for an answer and I could not find anything similar (or I didn't know the right words to search).

I have three columns of data which will be my x, y and z variable.

I want a graph that can show the values for x and y axis (as in the first and second column variables). However, I want the "points" (as a scatter plot) in the graph to be the values shown in variable z. Is there a way of doing that? Everything that I have tried plot x against y.

Thanks for any help!

like image 702
Fabs Avatar asked Jul 06 '13 02:07

Fabs


People also ask

Can you do a scatter plot with 3 variables?

As we said, a Scatter Plot is one of the best-suited visualization designs for displaying causal relationships in data with 3 variables. The visualization design uses a series of dots to display insights into two varying sets of data.

How do you graph the relationship between three variables?

There are a number of ways to show the relationship between three variables. One of the most common ways this is done is to add a third variable to a scatter plot of and two continuous variables. The third variable would be mapped to either the color, shape, or size of the observation point.


2 Answers

I believe this is what you are asking: Map two variables: (x,y) in their axis and display the "text" of a third variable. Let's use this data frame - We'll try to "write" X1 and X3

df <- data.frame(X1 = 1:5, X2 = 2*1:5, X3 = rnorm(1:5))

With base graphics you can just plot one character

plot(df$X1, df$X2, pch = paste(df$X1)) plot(df$X1, df$X2, pch = paste(df$X3)) 

doesn't seem to work well.

Using ggplot2:

ggplot(df, aes(x = X1, y = X2)) + geom_text(label = df$X1)
ggplot(df, aes(x = X1, y = X2)) + geom_text(label = df$X3)

a fancier alternative is adding colour in the aes()

ggplot(df, aes(x = X1, y = X2, color=X3)) + geom_text(label = df$X3)
like image 194
marbel Avatar answered Nov 08 '22 07:11

marbel


I want the "points" (as a scatter plot) in the graph to be the values shown in variable z. Is there a way of doing that?

Definitely. The bit that you need to think about is how to present the data in your z variable. By that I mean do you want the information in z to be shown by the points' colour, size or area? There are some great examples of how to do this at the R cookbook.

If you have a data frame called my.data, which has columns x, y, and z, you need to set up your plot like this:

my.plot <- ggplot(data = my.data,
                  aes(x = x,
                      y = y))

The example above says "plot the data in my.data using my.data$x to set the x location and y.data$y to set the y location". If your x variable was grid.x and y was grid.y you would have

my.plot <- ggplot(data = my.data,
                  aes(x = grid.x,
                      y = grid.y))

then you need to add your points. This time we'll assume that the information in z is going to used to set the colour of the points, which in this case is the colour aesthetic:

my.plot <- my.plot + geom_point(aes(colour = z))
print(my.plot)

And that should be that. You don't need to tell geom_point() what x and y are, because you already did that when you set up the plot.

like image 40
Andy Clifton Avatar answered Nov 08 '22 07:11

Andy Clifton