在ggplot2中指定颜色,轴线和背景的删除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ggplot2中指定颜色,轴线和背景的删除相关的知识,希望对你有一定的参考价值。
我在哪里以及如何在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")
答案
你可能想要这样的东西:
# 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())
当你想要干净的情节时,我使用了theme_classic()
,因为它完成了大部分工作。和scale_fill_manual(values = c("grey40", "grey60"))
指定颜色
以上是关于在ggplot2中指定颜色,轴线和背景的删除的主要内容,如果未能解决你的问题,请参考以下文章