r - 更改R中散点图中一个点或几个点的形状
问题描述
我在散点图中有一组点,如下所示。我想改变一点或几个点的形状。我搜索了这个,但找不到办法。
我想实现这样
像这样
代码:
df <- data.frame(x = c(1,2,2,3,3.5,4,4.5,5,5.5,6,1.5,2,2,2,2,1.5,2.5,3,3,3,3,5.5,5,6,5.5,7)
,y = c(2,1,2,2,2,2,2,2,1.5,2,2.5,3,3.5,4,4.5,3.5,3.5,2,3,3.5,4,2.5,3,3,4,3.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, shape = 3)+
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"))
解决方案
这怎么样?
使用 以 x 坐标为条件创建一个新列dplyr::mutate
(例如,但它可以是任何东西)。然后,使用此列aes
来控制形状大小。
此外,您可以使用scale_shape_manual
和scale_colour_manual
手动控制形状和颜色。我不清楚你想要什么形状,但你只需要更改scale_shape_manual
.
编辑:
由于您特别需要不同的符号,因此您需要geom_text
改用。
df %>%
dplyr::mutate(z = ifelse(x >= 5, "-", "+")) %>%
ggplot(aes(x, y)) +
geom_text(size = 12, aes(colour=z, label=z)) +
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")) +
scale_shape_manual(values=c(8, 9)) +
scale_colour_manual(values = c('red', 'blue'))
推荐阅读
- angular - 一张一张地重现base64编码的音频列表
- jquery - Facebook,它如何使用 ajax 并且只在调用时在用户端更新?
- python - 使用 python-lz4 解压未知长度的内容
- regex - 通过匹配组 id 进行广义替换
- python - 遍历嵌套的类似字典的结构,跟踪从根到叶的每条路径(Python)
- html - 在不触及 CSS 的情况下更改动态生成的输入大小
- php - 使用 PDO 的 SQL 中的错误顺序
- c# - 如何链接两个用户控件?WPF
- doxygen - doxygen:在标题中禁用 PROJECT_NAME
- list - Dart,从列表中获取所有 n 个数字字母值