[英]Plotting a third variable against x and y axis
I am very new to R and I am trying to plot a third variable to a plot using ggplot2. 我是R的新手,我正在尝试使用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. 我有三列数据,它们将是我的x,y和z变量。
I want a graph that can show the values for x and y axis (as in the first and second column variables). 我想要一个可以显示x和y轴值的图形(如第一和第二列变量中所示)。 However , I want the "points" (as a scatter plot) in the graph to be the values shown in variable z.
但是 ,我希望图中的“点”(作为散点图)是变量z中显示的值。 Is there a way of doing that?
有没有办法做到这一点? Everything that I have tried plot x against y.
我试过的所有东西都是针对y绘制的。
Thanks for any help! 谢谢你的帮助!
I believe this is what you are asking: Map two variables: (x,y) in their axis and display the "text" of a third variable. 我相信这就是你要问的:在它们的轴上映射两个变量:(x,y)并显示第三个变量的“文本”。 Let's use this data frame - We'll try to "write" X1 and X3
让我们使用这个数据框 - 我们将尝试“写”X1和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: 使用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() 一个更好的选择是在aes()中添加颜色
ggplot(df, aes(x = X1, y = X2, color=X3)) + geom_text(label = df$X3)
I want the "points" (as a scatter plot) in the graph to be the values shown in variable z.
我希望图中的“点”(作为散点图)是变量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. 您需要考虑的是如何在
z
变量中显示数据。 By that I mean do you want the information in z to be shown by the points' colour, size or area? 我的意思是你是否希望z中的信息由点的颜色,大小或区域显示? There are some great examples of how to do this at the R cookbook .
在R食谱中有一些很好的例子可以做到这一点。
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.data
的数据框,它有x
, y
和z
列,你需要像这样设置你的图:
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". 上面的例子说“使用
my.data$x
绘制my.data
的数据来设置x位置,使用y.data$y
来设置y位置”。 If your x variable was grid.x
and y was grid.y
you would have 如果您的x变量是
grid.x
而y是grid.y
那么您将拥有
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: 这次我们假设
z
中的信息将用于设置点的颜色,在这种情况下是colour
审美:
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. 您不需要告诉
geom_point()
x和y是什么,因为您在设置绘图时已经这样做了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.