英文:
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。为什么会这样?谢谢!
英文:
I drawed a plot with points and a horizontal line. Below is the example code:
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)
# Unexpected horizontal line
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()
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!
答案1
得分: 2
由于您的format
会将Value
转换为字符类型,因此ggplot
不再将其识别为连续变量。您需要使用数字y轴绘制图表,然后调整文本标签和y轴间断点以获得所需的格式(请注意,我创建了一个用于存储格式化字符Value
的Value2
列)。
library(ggplot2)
dt <- data.frame(ID = c("Line",
"P1",
"P2",
"P3"),
Year = "2016",
Value = c(1, 0.533, 1.233, 0.233))
dt$Value2 <- format(dt$Value, digits = 2)
# Unexpected horizontal line
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 = Value2,
x = Year, y = Value)) +
geom_hline(yintercept = subset(dt, ID == "Line")$Value,
color = "red") +
scale_y_continuous(breaks = as.numeric(dt$Value2[dt$ID != "Line"])) +
theme_bw()
<!-- -->
<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 <- data.frame(ID = c("Line",
"P1",
"P2",
"P3"),
Year = "2016",
Value = c(1, 0.533, 1.233, 0.233))
dt$Value2 <- format(dt$Value, digits = 2)
# Unexpected horizontal line
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 = Value2,
x = Year, y = Value)) +
geom_hline(yintercept = subset(dt, ID == "Line")$Value,
color = "red") +
scale_y_continuous(breaks = as.numeric(dt$Value2[dt$ID != "Line"])) +
theme_bw()
<!-- -->
<sup>Created on 2023-05-26 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论