英文:
Different graph results when using ggplot vs when using base R functions?
问题
I am creating some simple graphs for a project in R. I got unexpected results when using the ggplot2 package, and so I defaulted back to base R. When recreating the plot in base R, I got the correct result. Now I am just wondering why different results were produced?
Initially, this was the code and result using ggplot (which is incorrect):
ggplot(KBUF, aes(x = TEMP, y = HGHT)) +
geom_line(aes(color = "Temperature"), size = 1) +
geom_line(aes(x = DEWP, color = "Dew Point"), size = 1) +
scale_color_manual(values = c("blue", "red")) +
labs(x = "Temperature (°C)", y = "Height")
The correct plot I made in base R:
has the following code:
plot(KNKX$TEMP, KNKX$HGHT, type = "l", col = "blue", xlab = "Temperature (°C)", ylab = "Height")
lines(KNKX$DEWP, KNKX$HGHT, col = "red")
legend("topright", legend = c("Temperature", "Dew Point"), col = c("blue", "red"), lty = 1)
Why are they different?
英文:
I am creating some simple graphs for a project in R. I got unexpected results when using the ggplot2 package, and so I defaulted back to base R. When recreating the plot in base R, I got the correct result. Now I am just wondering why different results were produced?
Initially, this was the code and result using ggplot (which is incorrect):
ggplot(KBUF, aes(x = TEMP, y = HGHT)) +
geom_line(aes(color = "Temperature"), size = 1) +
geom_line(aes(x = DEWP, color = "Dew Point"), size = 1) +
scale_color_manual(values = c("blue", "red")) +
labs(x = "Temperature (°C)", y = "Height")
The correct plot I made in base R
has the following code:
plot(KNKX$TEMP, KNKX$HGHT, type = "l", col = "blue", xlab = "Temperature (°C)", ylab = "Height")
lines(KNKX$DEWP, KNKX$HGHT, col = "red")
legend("topright", legend = c("Temperature", "Dew Point"), col = c("blue", "red"), lty = 1)
Why are they different?
答案1
得分: 5
geom_line
会取所有的(x, y)坐标点,并按x值从小到大的顺序排列,然后通过这些点绘制一条线。如果您想按照它们出现的顺序绘制线条,需要使用geom_path
而不是geom_line
。
例如,假设我有一个数据框,其中包含按逆时针顺序排列的圆的坐标:
df <- data.frame(x = sin(pi * seq(-1, 1, 0.01)),
y = cos(pi * seq(-1, 1, 0.01)))
如果我使用geom_line
来绘制这个数据,会得到一团乱线,因为线会按照它们的x值连接所有的点:
library(ggplot2)
ggplot(df, aes(x, y)) + geom_line()
如果我想保持顺序,只需使用geom_path
而不是geom_line
:
ggplot(df, aes(x, y)) + geom_path()
英文:
geom_line
takes all the (x, y) co-ordinates and orders them from smallest x value to largest x value, then draws a line through these points. If you want to draw a line that goes through the points in the order they appear you need to use geom_path
, not geom_line
For example, suppose I have a data frame that has the co-ordinates of a circle ordered counter-clockwise:
df <- data.frame(x = sin(pi * seq(-1, 1, 0.01)),
y = cos(pi * seq(-1, 1, 0.01)))
If I plot this with geom_line
, I get a mess, because the line joins all the points according to their x value:
library(ggplot2)
ggplot(df, aes(x, y)) + geom_line()
If I want to preserve ordering, I simply use geom_path
instead of geom_line
:
ggplot(df, aes(x, y)) + geom_path()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论