在ggplot2中指定颜色,轴线和背景的删除

问题描述 投票:1回答:1

我在哪里以及如何在geombar中指定颜色,轴线和背景?最终,我希望有一个条形为深灰色,一个条形为浅灰色。它们目前是蓝色和粉红色,是默认值。我还希望x和y有轴线,而图形没有灰色背景。我使用下面的代码弄明白了。谢谢您的帮助。

library(ggplot2)
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = myData$mean + myData$se,
              ymin = myData$mean - myData$se)

p <- ggplot(data = myData, aes(x = names, y = mean, fill = names)) +

p + geom_bar(stat = "identity", position = dodge) +
  geom_errorbar(limits, position = dodge, width = 0.9) +
  theme(axis.text.x=element_blank(), axis.ticks.x=element_blank(),
        axis.title.x=element_blank())
limits <- aes(ymax = myData$mean + myData$se,
              ymin = myData$mean - myData$se)

p <- ggplot(data = myData, aes(x = factor(site), y = mean,
                               fill = factor(infectionstatus)))

p + geom_bar(stat = "identity",
         position = position_dodge(0.9)) +
  geom_errorbar(limits, position = position_dodge(0.9),
            width = 0.25) +
  labs(x = "Sites", y = "Average Calories in White Muscle Tissue")  +
  scale_fill_discrete(name = "Infection Status") 
r ggplot2 colors formatting geom-bar
1个回答
1
投票

你可能想要这样的东西:

# Generate data
myData <- data.frame(names = letters[1:2],
                     mean  = 1:2,
                     SE    = 0.1)

# Plot data
library(ggplot2)
ggplot(myData, aes(names, mean)) +
    geom_bar(aes(fill = names),
             stat = "identity", position = "dodge") +
    geom_errorbar(aes(ymin = mean - SE, ymax = mean + SE),
            position = position_dodge(width = 0.5), width = 0.5) +
    labs(title = "Calorie Amount",
         subtitle = "Averaged per Tissue",
         x = NULL, 
         y = "Average Calories in White Muscle Tissue",
         fill = "Infection Status") +
    scale_fill_manual(values = c("grey40", "grey60")) +
    theme_classic() +
    theme(axis.text.x = element_blank(), 
          axis.ticks.x = element_blank(),
          axis.title.x = element_blank())

enter image description here

当你想要干净的情节时,我使用了theme_classic(),因为它完成了大部分工作。和scale_fill_manual(values = c("grey40", "grey60"))指定颜色

© www.soinside.com 2019 - 2024. All rights reserved.