“ggplot2″产生意外的水平线。

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

Unexpected horizontal line by ggplot2

问题

我画了一个带点和水平线的图。以下是示例代码:

dt <- data.frame(ID = c("Line",
                        "P1",
                        "P2",
                        "P3"),
                 Year = "2016",
                 Value = c(1, 0.533, 1.233, 0.233))

dt$Value <- format(dt$Value, digits = 2)

library(ggplot2)
# 意外的水平线
ggplot(NULL) +
  geom_point(data = subset(dt, ID != "Line"),
             aes(x = Year, y = Value, color = ID, shape = ID)) + 
  geom_text(data = subset(dt, ID != "Line"),
            aes(label = subset(dt, ID != "Line")$Value,
                x = Year, y = Value)) +
  geom_hline(yintercept = as.numeric(subset(dt, ID == "Line")$Value),
             color = "red") +
  theme_bw()

然而,下面的图似乎有点奇怪:红色的水平线位于y轴0.23而不是1。为什么会这样?谢谢!

“ggplot2″产生意外的水平线。

英文:

I drawed a plot with points and a horizontal line. Below is the example code:

dt &lt;- data.frame(ID = c(&quot;Line&quot;,
                        &quot;P1&quot;,
                        &quot;P2&quot;,
                        &quot;P3&quot;),
                 Year = &quot;2016&quot;,
                 Value = c(1, 0.533, 1.233, 0.233))

dt$Value &lt;- format(dt$Value, digits = 2)

library(ggplot2)
# Unexpected horizontal line
ggplot(NULL) +
  geom_point(data = subset(dt, ID != &quot;Line&quot;),
             aes(x = Year, y = Value, color = ID, shape = ID)) + 
  geom_text(data = subset(dt, ID != &quot;Line&quot;),
            aes(label = subset(dt, ID != &quot;Line&quot;)$Value,
                x = Year, y = Value)) +
  geom_hline(yintercept = as.numeric(subset(dt, ID == &quot;Line&quot;)$Value),
             color = &quot;red&quot;) +
  theme_bw()

However, the Figure below seems strange: the red horizontal line is on the y-axis of 0.23, not 1. Why does this happen? Thanks!

“ggplot2″产生意外的水平线。

答案1

得分: 2

由于您的format会将Value转换为字符类型,因此ggplot不再将其识别为连续变量。您需要使用数字y轴绘制图表,然后调整文本标签和y轴间断点以获得所需的格式(请注意,我创建了一个用于存储格式化字符ValueValue2列)。

library(ggplot2)

dt &lt;- data.frame(ID = c(&quot;Line&quot;,
                        &quot;P1&quot;,
                        &quot;P2&quot;,
                        &quot;P3&quot;),
                 Year = &quot;2016&quot;,
                 Value = c(1, 0.533, 1.233, 0.233))

dt$Value2 &lt;- format(dt$Value, digits = 2)

# Unexpected horizontal line
ggplot(NULL) +
  geom_point(data = subset(dt, ID != &quot;Line&quot;),
             aes(x = Year, y = Value, color = ID, shape = ID)) + 
  geom_text(data = subset(dt, ID != &quot;Line&quot;),
            aes(label = Value2,
                x = Year, y = Value)) +
  geom_hline(yintercept = subset(dt, ID == &quot;Line&quot;)$Value,
             color = &quot;red&quot;) +
  scale_y_continuous(breaks = as.numeric(dt$Value2[dt$ID != &quot;Line&quot;])) +
  theme_bw()

“ggplot2″产生意外的水平线。<!-- -->

<sup>Created on 2023-05-26 with reprex v2.0.2</sup>

英文:

Because your format would turn Value into character type, therefore ggplot no longer recognise it as continuous. You need to plot the graph using a numeric y-axis, then play around with the text labels and y-axis breaks for your desired formatting (note I created a Value2 column for storing formatted character Value).

library(ggplot2)

dt &lt;- data.frame(ID = c(&quot;Line&quot;,
                        &quot;P1&quot;,
                        &quot;P2&quot;,
                        &quot;P3&quot;),
                 Year = &quot;2016&quot;,
                 Value = c(1, 0.533, 1.233, 0.233))

dt$Value2 &lt;- format(dt$Value, digits = 2)

# Unexpected horizontal line
ggplot(NULL) +
  geom_point(data = subset(dt, ID != &quot;Line&quot;),
             aes(x = Year, y = Value, color = ID, shape = ID)) + 
  geom_text(data = subset(dt, ID != &quot;Line&quot;),
            aes(label = Value2,
                x = Year, y = Value)) +
  geom_hline(yintercept = subset(dt, ID == &quot;Line&quot;)$Value,
             color = &quot;red&quot;) +
  scale_y_continuous(breaks = as.numeric(dt$Value2[dt$ID != &quot;Line&quot;])) +
  theme_bw()

“ggplot2″产生意外的水平线。<!-- -->

<sup>Created on 2023-05-26 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月26日 11:09:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337414.html
匿名

发表评论

匿名网友

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

确定