R语言 有人能给我一个建议吗?为什么我的函数不提供带有定制点的图形,而是提供黑点?

gcxthw6b  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(87)

这是我创建颜色的函数:

create_color <- function(df){
  level <- create_level(df)
  colors <- list ()

  for (i in 1:length(level)) {
    ilevel <- level [i]
    
       if (any(grepl("5min", ilevel))) {
      colors[i] <- "#77003c"
    } else if (any(grepl("60min", ilevel))) {
      colors[i] <- "#9804e4"
    } else if (any(grepl("6h", ilevel))) {
      colors[i] <- "#bd7ebd"
    } else if (any(grepl("24h", ilevel))) {
      colors[i] <- "#a49787"
    } else {
      colors[i] <- "#000000"
    }
  }
  return (colors)
}

这是plot函数:

histo_plot_dotty <- function(df, min, max){
  colors <- create_color(df)
  ylabelling <- as.character(df[[1,3]])
  print(colors)
  level <- create_level(df)
  print(level)
  plot <- ggplot(df, aes (x = factor(key, level = level), y = values), fill = group_column2)+
  geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 0.3, binpositions = "all", dotsize = 1)+
    scale_fill_manual (values = colors)+
    scale_y_continuous(limits = c(min, max), expand = c(0.01, 0)) +
    labs(title = "",
         x = "individual",
         y = paste0("(", ylabelling, ")","/FoV"),
         fill = "dosing") +
    theme(axis.text.x = element_text(angle = -65, vjust = 0.5, hjust=0, size = 8),
          panel.background = element_blank(),
          axis.line = element_line(color = "black"))
  print(plot)
    print(plot)
    return(plot)
}

this is what the dataframe that I apply both functions on looks like:
我尝试了不同版本的create_color()函数,其中将其包含到plot函数中,通过颜色列扩展df dataframe,并使用scale_fill_manual()函数读取,scale_colour_manual()代替fill,将颜色包含到geom函数中,等等。

w6lpcovy

w6lpcovy1#

我们必须将colorsMap到fill美学。一个例子,代码改编自问题:

library(ggplot2)

df <- iris
colors <- c('red', 'blue')
ggplot(df,
       aes(x = factor(Species),
           y = Sepal.Width,
           fill = rep(colors, each = 75
                      )
           )
       ) +
    geom_dotplot(binaxis = "y",
                 stackdir = "center",
                 stackratio = 0.3,
                 binpositions = "all",
                 dotsize = 1
                 )

相关问题