使用ggplot和使用基本的R函数时,图形结果会有何不同?

huangapple go评论53阅读模式
英文:

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")

使用ggplot和使用基本的R函数时,图形结果会有何不同?

The correct plot I made in base R:

使用ggplot和使用基本的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")

使用ggplot和使用基本的R函数时,图形结果会有何不同?

The correct plot I made in base R

使用ggplot和使用基本的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()

使用ggplot和使用基本的R函数时,图形结果会有何不同?

英文:

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 &lt;- 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()

使用ggplot和使用基本的R函数时,图形结果会有何不同?

If I want to preserve ordering, I simply use geom_path instead of geom_line:

ggplot(df, aes(x, y)) + geom_path()

使用ggplot和使用基本的R函数时,图形结果会有何不同?

huangapple
  • 本文由 发表于 2023年6月5日 05:03:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定