简体   繁体   English

在 R 中更改散点图中一个点或几个点的形状 plot

[英]Changing the shape of one point or few points in a scatter plot in R

I have a set of points in a scatter plot as below.我在散点图 plot 中有一组点,如下所示。 I want to change the shape of one point or few points.我想改变一点或几个点的形状。 I searched for this but could not find a way to do it.我搜索了这个,但找不到办法。

原来的

I want to achieve like this我想实现这样

分配

And like this像这样

图像3

Code:代码:

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

How is this?这怎么样?

Create a new column using dplyr::mutate , which is conditional upon the x-coordinates (for example, but it could be anything).使用dplyr::mutate创建一个新列,它以 x 坐标为条件(例如,但它可以是任何东西)。 Then, use this column within aes to control the shape size.然后,使用aes中的此列来控制形状大小。

Also, you can use scale_shape_manual and scale_colour_manual to manually control the shape and colours.此外,您可以使用scale_shape_manualscale_colour_manual手动控制形状和颜色。 It's not clear to me what shape you want but you would just need to change the arguments in scale_shape_manual .我不清楚您想要什么形状,但您只需要更改 scale_shape_manual 中的scale_shape_manual

EDIT:编辑:

Since you specifically need a different symbol, then you need to use geom_text instead.由于您特别需要不同的符号,因此您需要使用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'))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM