【问题标题】:How to colour my scatter plot with data that I've combined?如何用我组合的数据为我的散点图着色?
【发布时间】:2020-08-25 15:10:31
【问题描述】:

我有两个数据集(我最终将使用八个),我将它们组合起来创建了一个散点图。问题是,现在我已经绘制了散点图,我不知道如何分离我组合的数据,因此颜色代表各个数据集。这是我的代码...

#Here is what I've combined:

    t<-rbind(test202, test342)

#Here is plotting the scatter-plot 

    ```{r}
    g<-ggplot(t,aes(x=percentage,y=as.numeric(area), col = area, group = area ))+
      geom_point()+
  labs(x=expression(paste("Percentage (%)")),
       y=expression(paste("Area (m"^2,")"))
       )+
  scale_y_continuous(breaks = seq(0, 20, by = 1)) +
  theme_bw() +
  theme(element_blank())
g
``

我试过用谷歌搜索,但似乎找不到任何特定的代码来解决这个问题。我附上了一张最终产品和't's内容的图片。 contents of 't'scatter-plot of 't'

编辑:输入(t)

t <- structure(list(percentage = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L), .Label = c("30", "35", 
    "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", 
    "95"), class = "factor"), area = c(1.0068507612755, 1.28144642344154, 
    1.55604208560758, 1.92216963516231, 2.28829718471704, 2.65442473427176, 
    3.20361605860385, 3.75280738293594, 4.39353059465671, 5.30884946854352, 
    6.49876400459638, 7.96327420281528, 10.068507612755, 13.6382512209135, 
    1.12935650675177, 1.4004020683722, 1.67144762999262, 1.98766745188312, 
    2.34906153404369, 2.75562987647432, 3.16219821890496, 3.65911508187574, 
    4.15603194484652, 4.74329732835744, 5.37573697213844, 6.18887365699971, 
    7.22788164321134, 8.89932927320397)), row.names = c(NA, -28L), class = "data.frame")

【问题讨论】:

  • t 在哪里?重现您的问题需要数据!
  • 我附上了照片,如果你能看到?你是这个意思吗?谢谢
  • 您能否在控制台中执行此操作:dput(t) 并将输出粘贴到问题中?
  • 是的,我会这样做,谢谢

标签: r colors


【解决方案1】:

当您rbind 将这些行放在一起时,您似乎只是忘记了数据的来源。既然你说你最终有更多的 dfs 加入,我会提供一个dplyr 解决方案。

library(dplyr)
library(ggplot2)

t <- dplyr::bind_rows(list(test202 = test202,
                           test342 = test342),
                      .id = 'source')

ggplot(t, aes(x = percentage,
              y = as.numeric(area),
              col = source,
              group = source ))+
   geom_point()+
   labs(x = expression(paste("Percentage (%)")),
        y = expression(paste("Area (m"^2,")"))
   )+
   scale_y_continuous(breaks = seq(0, 20, by = 1)) +
   theme_bw() +
   theme(element_blank())

【讨论】:

  • 谢谢你。将来当我添加更多数据时,我会继续'bind_rows(list())'并进行更多测试吗?
  • 没错,只要继续添加到您要合并的数据框列表中即可。顺便说一句,您可以随意命名它们(在等式的左侧)。如果这解决了您的问题,请标记复选标记。
  • 非常感谢!非常感谢(:
猜你喜欢
  • 2021-02-17
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
  • 2018-01-07
相关资源
最近更新 更多