[英]Generating LaTeX style scatter plot diagram using R ggplot2
I would like to generate a Latex diagram (a scatter plot) using ggplot2 in R.我想在 R 中使用 ggplot2 生成 Latex 图(散点图)。
Original image:原图:
Code:代码:
df <- data.frame(x = c(1,1.5,1.5,2,2,2,2,2,2,2.5,3,3,3,3,3.5,4,4.5,5,5,5.5,5.5,5.5,6,6,7)
,y = c(1,1.5,2,2,2,2,2,2,2,2,2.5,2.5,3,3,3,3,3.5,3.5,3.5,3.5,3.5,4,4,4,4.5))
ggplot(df,aes(x=x,y=y)) + geom_point(alpha = 0.3)
What I obtain:我得到的:
One workaround would be:一种解决方法是:
ggplot(df,aes(x=x,y=y)) + geom_point(col = "blue", size = 5) +
theme_minimal() +
labs(x="", y="") +
annotate(geom = "text", x = 1, y = 5, label = "y") +
annotate("text", x = 7, y = 1, label = "x") +
theme(axis.line.y = element_line(arrow = grid::arrow(length = unit(0.4, "cm"))),
axis.line.x = element_line(arrow = grid::arrow(length = unit(0.4, "cm"))))
You could try this which very nearly gets you all the way there:你可以试试这个,它几乎可以让你一路走到那里:
Shortfalls:不足之处:
df <- data.frame(x = c(1,1.5,1.5,2,2,2,2,2,2,2.5,3,3,3,3,3.5,4,4.5,5,5,5.5,5.5,5.5,6,6,7)
,y = c(1,1.5,2,2,2,2,2,2,2,2,2.5,2.5,3,3,3,3,3.5,3.5,3.5,3.5,3.5,4,4,4,4.5))
library(ggplot2)
library(extrafont)
# helper dataframe for axis
df_arrow <- data.frame(x = c(0, 0),
y = c(0, 0),
xend = c(0, 8),
yend = c(8, 0))
ggplot(df,aes(x, y)) +
geom_point(colour = "blue", size = 5)+
scale_x_continuous(breaks = 1:7, expand = expansion(add = c(0, 1)))+
scale_y_continuous(breaks = 1:7, expand = expansion(add = c(0, 1)))+
coord_fixed(xlim = c(0, 7), ylim = c(0, 7), clip = "off")+
geom_segment(data = df_arrow, aes(x = x, xend = xend, y = y, yend = yend), size = 0.75, colour = "black",
arrow = arrow(angle = 20, length = unit(3, "mm"), ends = "last", type = "closed"), linejoin = "mitre") +
annotate("text", x = c(7.8, 0.3), y = c(0.3, 7.8), label = c("italic(x)", "italic(y)"), parse = TRUE, size = 6, family = "Times New Roman")+
labs(x = NULL,
y = NULL)+
theme_bw()+
theme(panel.grid.major = element_line(colour = "gray80"),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.ticks.length = unit(1, "mm"),
text = element_text(size = 18, family = "Times New Roman"))
Created on 2021-05-22 by the reprex package (v2.0.0)由代表 package (v2.0.0) 于 2021 年 5 月 22 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.