0

我试图用一条线连接我的点,就像这样,但对于我的情节:

在此处输入图像描述

我的情节是这样的:

在此处输入图像描述

我试过使用 geom_segment 但它没有用。我的基本代码是:

ggplot(tidydf, aes(Genome_size, `Trio_number`, color = Group)) +
     geom_point() + scale_y_continuous(breaks = seq(0, 20, by = 1)) +
     ylab("Trio number") + xlab("Genome size (kb)") + theme_dotplot + scale_x_continuous(labels = comma) + theme(legend.position = "bottom") + scale_color_brewer(palette = "Set1") + theme(legend.title=element_blank()) + guides(colour = guide_legend(override.aes = list(size=4))) theme(legend.key=element_rect(fill='gray96')) +
     theme(plot.background = element_rect(fill = 'gray96')) + theme(legend.title = element_text(size=10)) +  theme(text=element_text(size=12,  family="Gujarati Sangam MN")) + theme(axis.title.x = element_text(vjust = 0, size = 11), axis.title.y = element_text(vjust = 2, size = 11)) + theme(axis.text = element_text(color = "black", size = 9))

我将如何使用 geom_segment 为我的数据执行此操作?谢谢你的帮助!

更新:可重现的代码

library(ggplot2)
library(scales)

set.seed(8675309)

tidydf <- data.frame(
  Genome_size = sample(1000:7000, 30, replace = T),
  Trio_number = sample(1:20, 30, replace = T),
  Group = sample(c('Free-living', 'Gut', 'Pathogen'), 30, replace = T)
)

p <- 
ggplot(tidydf, aes(Genome_size, `Trio_number`, color = Group)) +
  geom_point() +
  scale_y_continuous(breaks = seq(0, 20, by = 1)) +
  ylab("Trio number") + xlab("Genome size (kb)") +
  theme_light() +
  scale_x_continuous(labels = comma) +
  scale_color_brewer(palette = "Accent") +
  guides(colour = guide_legend(override.aes = list(size=4))) +
  theme(
    legend.position = "bottom",
    legend.key=element_rect(fill='gray96'),
    plot.background = element_rect(fill = 'gray96'),
    legend.title =element_text(size=10),
    text=element_text(size=12),
    axis.title.x = element_text(vjust = 0, size = 11),
    axis.title.y = element_text(vjust = 2, size = 11),
    axis.text = element_text(color = "black", size = 9),
    
    # to make the theme look more similar to OP example
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank()
  )
p

更新:我的数据

structure(list(Trio_number = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 
4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 
11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 
16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20), `Accession ID` = c("NC_007530.2", 
"NC_000964.3", "NZ_CP053376.1", "NZ_CP061527.1", "NZ_CP009756.1", 
"NZ_CP050508.1", "NZ_CP010820.1", "NC_021181.2", "NZ_CP009909.1", 
"NZ_LS483427.1", "NZ_CP026999.1", "NC_014151.1", "NZ_CP019870.1", 
"NZ_CP030775.1", "NC_015687.1", "NZ_CP024727.1", "NZ_CP040530.1", 
"NZ_CP032489.1", "NZ_CP022389.1", "NZ_CP054012.1", "NC_014734.1", 
"NZ_AP014857.1", "NZ_CP060111.1", "NC_008576.1", "NC_003103.1", 
"NC_016845.1", "NZ_CP026328.2", "NC_003197.2", "NZ_CP033744.1", 
"NC_014414.1", "NC_004557.1", "NZ_CP030777.1", "NC_014624.2", 
"NC_000962.3", "NZ_CP028341.1", "NC_013124.1", "NZ_CP029543.1", 
"NZ_CP006712.1", "NC_013739.1", "NZ_AP022576.1", "NZ_AP012326.1", 
"NC_009664.2", "NC_008593.1", "NZ_LR699011.1", "NZ_LR134338.1", 
"NZ_CP028842.1", "NZ_CP036345.1", "NC_016584.1", "NC_011725.1", 
"NZ_CP012938.1", "NZ_CP023665.1", "NZ_LS483376.1", "NZ_LN877293.1", 
"NZ_LT605205.1", "NZ_CP014150.1", "NZ_CP039729.1", "NC_016052.1", 
"NZ_CP008747.1", "NZ_CP023011.2", "NC_018631.1"), Group = c("Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living", "Pathogen", 
"Gut", "Free-living", "Pathogen", "Gut", "Free-living"), Genome_size = c(5227.419, 
4215.606, 4026.648, 4393.662, 4848.754, 5598.454, 4843.789, 1991.579, 
5762.608, 1988.85, 2365.873, 4123.179, 4124.384, 3810.128, 3942.462, 
2011.558, 6271.157, 4099.663, 2455.405, 4926.033, 3685.504, 4657.167, 
6149.586, 4719.581, 1268.755, 5333.942, 2726.366, 4857.45, 4974.986, 
2902.643, 2799.251, 2970.937, 4316.707, 4411.532, 2192.428, 2158.157, 
3187.112, 2288.919, 6359.369, 6219.859, 2635.669, 4761.183, 2547.72, 
3592.125, 6726.998, 3858.511, 3590.716, 5863.081, 5419.036, 6472.489, 
4376.831, 3874.791, 5188.967, 4414.963, 3550.458, 2736.723, 2562.72, 
2472.129, 2845.651, 1893.499)), row.names = c(NA, -60L), class = c("tbl_df", 
"tbl", "data.frame"))

我会试试geom_line!谢谢

4

1 回答 1

2

感谢您发布您的示例。在这种情况下,@stefan 有正确的答案。只需使用geom_line()并赋予group=您 Trio_number 的美感。group当您想使用列进行区分但不希望每个组看起来不同时,您可以使用审美修饰符。

在您的问题中,您需要单独的行,按 分组Trio_number,但它们都应该是相同的颜色。因此,您需要做的就是将以下行添加到您的绘图代码中:

# plot code before geom_point()... +

  geom_line(aes(group=Trio_number), color='black') +

# geom_point() and all the rest of the plot code

在此处输入图像描述

于 2021-03-21T12:49:14.717 回答