ggplot:修改直方图

发布于 2025-01-11 00:50:06 字数 1152 浏览 0 评论 0原文

我使用以下代码制作了此图:

ggplot(all, aes(x = year, color = layer)) +
  geom_histogram(binwidth = 0.5, fill = "white", alpha = 0.5, position = "dodge") +
  scale_x_continuous(breaks = pretty(all$year)) +
  scale_color_discrete(name = "title", labels = c("A","B")) +
  theme_light() +
  theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
        text = element_text(size = 20),
        axis.title.x = element_text(margin = margin(t = 25, r = 0, b = 0, l = 0)),
        axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),
        axis.text.x = element_text(angle = 50, hjust = 1, size = 18, color = "black"),
        axis.text.y = element_text(size = 18, color = "black"))

我现在想首先使用 viridis 调色板中的颜色更改颜色。此外,直方图之间有蓝色和红色的笔划,我想将其删除。 有人可以帮我更改代码吗? 提前致谢!

测试数据:

year <- runif(10, 2014, 2021)
year <- round(year, 0)
layer <- sample(c("A","B"), size=10, replace=T)
all <- as.data.frame(year,layer)

“在此处输入图像描述"

I made this plot using the following code:

ggplot(all, aes(x = year, color = layer)) +
  geom_histogram(binwidth = 0.5, fill = "white", alpha = 0.5, position = "dodge") +
  scale_x_continuous(breaks = pretty(all$year)) +
  scale_color_discrete(name = "title", labels = c("A","B")) +
  theme_light() +
  theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
        text = element_text(size = 20),
        axis.title.x = element_text(margin = margin(t = 25, r = 0, b = 0, l = 0)),
        axis.title.y = element_text(margin = margin(t = 0, r = 25, b = 0, l = 0)),
        axis.text.x = element_text(angle = 50, hjust = 1, size = 18, color = "black"),
        axis.text.y = element_text(size = 18, color = "black"))

I would now like to change the colors first, using colors from the viridis palette. Furthermore, there are blue and red strokes between the histograms, which I would like to remove.
Could someone help me to change the code?
Thanks in advance!

Test Data:

year <- runif(10, 2014, 2021)
year <- round(year, 0)
layer <- sample(c("A","B"), size=10, replace=T)
all <- as.data.frame(year,layer)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紧拥背影 2025-01-18 00:50:06

看起来你想要的是条形图而不是直方图。

all <- data.frame(year,layer) ## fix the sample data creation

ggplot(all, aes(x = year, fill = layer)) +  ## I think fill looks better...
  geom_bar(position = position_dodge(preserve = "single")) +  ## bar, not histogram
  #scale_x_continuous(breaks = pretty(all$year)) + ## this line just confirmed defaults
  scale_fill_viridis_d() +
  theme_light() ## omitted the rest of the theme as irrelevant for the issue at hand

输入图片这里的描述

如果您确实想要轮廓颜色,而不是填充,请切换到geom_bar“修复”条之间的笔画:

ggplot(all, aes(x = year, color = layer)) +
  geom_bar(position = position_dodge(preserve = "single"), fill = NA) +
  scale_color_viridis_d() +
  theme_light() 

在此处输入图像描述

Seems like you want a bar plot not a histogram.

all <- data.frame(year,layer) ## fix the sample data creation

ggplot(all, aes(x = year, fill = layer)) +  ## I think fill looks better...
  geom_bar(position = position_dodge(preserve = "single")) +  ## bar, not histogram
  #scale_x_continuous(breaks = pretty(all$year)) + ## this line just confirmed defaults
  scale_fill_viridis_d() +
  theme_light() ## omitted the rest of the theme as irrelevant for the issue at hand

enter image description here

If you do want outline color, not fill, switching to geom_bar "fixes" the strokes between the bars:

ggplot(all, aes(x = year, color = layer)) +
  geom_bar(position = position_dodge(preserve = "single"), fill = NA) +
  scale_color_viridis_d() +
  theme_light() 

enter image description here

沉默的熊 2025-01-18 00:50:06

谢谢,这是有用的信息!

Thank you, this is helpful information!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文