[英]Using ggplot2 how can I plot points with an aes() after plotting lines?
I'm using ggplot2 to show lines and points on a plot.我正在使用 ggplot2 来显示 plot 上的线和点。 What I am trying to do is to have the lines all the same color, and then to show the points colored by an attribute.
我想要做的是让所有线条都具有相同的颜色,然后显示由属性着色的点。 My code is as follows:
我的代码如下:
# Data frame
dfDemo <- structure(list(Y = c(0.906231077471568, 0.569073561538186,
0.0783433165521566, 0.724580209473378, 0.359136092118470, 0.871301974471722,
0.400628333618918, 1.41778205350433, 0.932081770977729, 0.198188442350644
), X = c(0.208755495088456, 0.147750173706688, 0.0205864576474412,
0.162635017485883, 0.118877260137735, 0.186538613831806, 0.137831912094464,
0.293293029083812, 0.219247919537514, 0.0323148791663826), Z = c(11112951L,
11713300L, 14331476L, 11539301L, 12233602L, 15764099L, 10191778L,
12070774L, 11836422L, 15148685L)), .Names = c("Y", "X", "Z"
), row.names = c(NA, 10L), class = "data.frame")
# Variables
X = array(0.1,100)
Y = seq(length=100, from=0, by=0.01)
# make data frame
dfAll <- data.frame()
# make data frames using loop
for (x in c(1:10)){
# spacemate calc
Floors = array(x,100)
# include label
Label = paste(' ', toString(x), sep="")
df1 <- data.frame(X = X * x, Y = Y, Label)
# merge df1 to cumulative df, dfAll
dfAll <- rbind(dfAll, df1)
}
# plot
pl <- ggplot(dfAll, aes(x = X, y = Y, group = Label, colour = 'Measures')) + geom_line()
# add points to plot
pl + geom_point(data=dfDemo, aes(x = X, y = Y)) + opts(legend.position = "none")
This almost works, but I am unable to color the points by Z
when I do this.这几乎可以工作,但是当我这样做时,我无法按
Z
为点着色。 I can plot the points separately, colored by Z
using the following code:我可以 plot 分别使用以下代码用
Z
着色点:
ggplot(dfDemo, aes(x = X, y = Y, colour = Z)) + geom_point()
However, if I use the similar code after plotting the lines:但是,如果我在绘制线条后使用类似的代码:
pl + geom_point(data=dfDemo, aes(x = X, y = Y, colour = Z)) + opts(legend.position = "none")
I get the following error:我收到以下错误:
Error: Continuous variable () supplied to discrete scale_hue.
I don't understand how to add the points to the chart so that I can colour them by a value.我不明白如何将点添加到图表中,以便我可以按值对它们进行着色。 I appreciate any suggestion how to solve this.
我很感激任何关于如何解决这个问题的建议。
The issue is that they are colliding the two colour scales, one from the ggplot call and the other from geom_point.问题是它们正在碰撞两个色标,一个来自 ggplot 调用,另一个来自 geom_point。 If you want the lines of one colour and the points of different colours then you need to erase the colour setting from ggplot call and put it inside the geom_line outside the aes call so it isn't mapped.
如果你想要一种颜色的线条和不同颜色的点,那么你需要从 ggplot 调用中删除颜色设置,并将其放在 aes 调用之外的 geom_line 内,这样它就不会被映射。 Use
I()
to define the colour otherwise it will think is just a variable.使用
I()
定义颜色,否则它会认为只是一个变量。
pl <- ggplot(dfAll, aes(x = X, y = Y, group = Label)) +
geom_line(colour = I("red"))
pl + geom_point(data=dfDemo, aes(x = X, y = Y, colour = Z)) +
opts(legend.position = "none")
HTH HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.