分享
三行代码  ›  专栏  ›  技术社区  ›  Geomicro

如何使所有数据点的颜色相同?

  •  0
  • Geomicro  · 技术社区  · 1 周前

    我希望这将是一个简单的解决方案,但如何为气泡图中的每个数据点指定一种颜色呢?当我指定 color= fill= color = "blue" )它只会变成红色。

    我甚至在数据框中添加了一个新列,其中所有行都具有相同的分类值“samecolor”,我将其赋值为 fill = samecolor scale_color_manual(values = c('mediumorchid') .

    我的完整代码在这里:

    p.class <- ggplot(data_melt, aes(x=pond, y=variable)) +
      geom_point(aes(size = value, fill = samecolor), alpha = 0.75, shape = 21) +
      scale_color_manual(values = c("mediumorchid")) +
      #scale_fill_discrete(values = c("mediumorchid")) +
      theme(legend.position = "bottom", legend.box = "horizontal", legend.direction = "horizontal",
            panel.background = element_blank()) +
      xlab("") +
      ylab("Taxonomy (Class)") +
      theme_linedraw(base_size = 18) +
      theme(axis.text.x = element_text(angle=45, hjust = 1)) +
      theme(plot.title = element_text( size = 15, face = "bold")) 
    

    scale_fill_discrete() 填充=同色 .

    提前谢谢你!

    > dput(data_melt)
    structure(list(pond = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L, 
    3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
    1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
    3L, 2L, 2L, 2L, 2L), .Label = c("GG", "LG", "SM"), class = "factor"), 
        variable = c("taxa1", "taxa1", "taxa1", "taxa1", "taxa1", 
        "taxa1", "taxa1", "taxa1", "taxa1", "taxa1", "taxa1", "taxa1", 
        "taxa2", "taxa2", "taxa2", "taxa2", "taxa2", "taxa2", "taxa2", 
        "taxa2", "taxa2", "taxa2", "taxa2", "taxa2", "taxa3", "taxa3", 
        "taxa3", "taxa3", "taxa3", "taxa3", "taxa3", "taxa3", "taxa3", 
        "taxa3", "taxa3", "taxa3", "taxa4", "taxa4", "taxa4", "taxa4", 
        "taxa4", "taxa4", "taxa4", "taxa4", "taxa4", "taxa4", "taxa4", 
        "taxa4", "taxa5", "taxa5", "taxa5", "taxa5", "taxa5", "taxa5", 
        "taxa5", "taxa5", "taxa5", "taxa5", "taxa5", "taxa5"), value = c(0.134328358208955, 
        0.00497512437810945, 0.00746268656716418, 0.0124378109452736, 
        0.054726368159204, 0.203980099502488, 0.114427860696517, 
        0.109452736318408, 0.154228855721393, 0.00746268656716418, 
        0.00497512437810945, 0.191542288557214, 0.0757575757575758, 
        0.0833333333333333, 0.113636363636364, 0.128787878787879, 
        0.0606060606060606, 0.0227272727272727, 0.121212121212121, 
        0.0303030303030303, 0.0303030303030303, 0.143939393939394, 
        0.159090909090909, 0.0303030303030303, 0.0797498045347928, 
        0.0867865519937451, 0.0781860828772478, 0.0766223612197029, 
        0.0750586395621579, 0.0594214229867084, 0.0969507427677873, 
        0.0953870211102424, 0.101641907740422, 0.0836591086786552, 
        0.0852228303362002, 0.0813135261923378, 0.0163934426229508, 
        0.0655737704918033, 0.0327868852459016, 0.0819672131147541, 
        0.0491803278688525, 0.114754098360656, 0.0983606557377049, 
        0.131147540983607, 0.147540983606557, 0.0819672131147541, 
        0.131147540983607, 0.0491803278688525, 0.088339222614841, 
        0.0918727915194346, 0.0848056537102474, 0.0812720848056537, 
        0.0989399293286219, 0.0777385159010601, 0.0742049469964664, 
        0.0777385159010601, 0.102473498233216, 0.0918727915194346, 
        0.0671378091872792, 0.0636042402826855), samecolor = c("color", 
        "color", "color", "color", "color", "color", "color", "color", 
        "color", "color", "color", "color", "color", "color", "color", 
        "color", "color", "color", "color", "color", "color", "color", 
        "color", "color", "color", "color", "color", "color", "color", 
        "color", "color", "color", "color", "color", "color", "color", 
        "color", "color", "color", "color", "color", "color", "color", 
        "color", "color", "color", "color", "color", "color", "color", 
        "color", "color", "color", "color", "color", "color", "color", 
        "color", "color", "color")), row.names = c(NA, -60L), class = "data.frame")
    
    1 回复  |  直到 1 周前
        1
  •  2
  •   Duck    1 周前

    试试这个。最好用 scale_fill_manual() 如果你想改变你的 fill 选项。你可以随意设定颜色。考虑到这一点 color 是不同的美学元素。这就是为什么你把西红柿的颜色改成原来的颜色,因为你想改变颜色 颜色 已打开。代码如下:

    library(ggplot2)
    #Code
    ggplot(data_melt, aes(x=pond, y=variable)) +
      geom_point(aes(size = value, fill = samecolor), alpha = 0.75, shape = 21) +
      scale_fill_manual(values = c("blue")) +
      #scale_fill_discrete(values = c("blue")) +
      theme(legend.position = "bottom", legend.box = "horizontal", legend.direction = "horizontal",
            panel.background = element_blank()) +
      xlab("") +
      ylab("Taxonomy (Class)") +
      theme_linedraw(base_size = 18) +
      theme(axis.text.x = element_text(angle=45, hjust = 1)) +
      theme(plot.title = element_text( size = 15, face = "bold")) 
    

    enter image description here