【问题标题】:change color of text and chart - ggplot2 in r更改文本和图表的颜色 - r 中的 ggplot2
【发布时间】:2021-08-21 14:06:42
【问题描述】:

我想让文本和 geom_area 填充特定的颜色。 颜色代码是:#795fc6
我试过了,但它不起作用,并且 fill=0.1 根本不是正确的颜色。

我的数据集包含 4662 个观察值的 26 个变量。 对于这个情节,我只需要两个变量。 anaylysis_date (日期格式 YYYY-MM-DD ) y 轴是分析期间每一天发生的所有观测值的总和。

这是分析日期列的可重现示例:

df = c("2017-11-12", "2018-03-28", "2018-06-30", "2018-06-22", "2018-09-24" ,"2018-11-21")

这是我此刻的情节:

ggplot(data = DatasetApp, aes(x=analysis_date, fill=0.1)) +
  geom_area(stat="bin",axis.text.x=col=c("#795fc6")) +
  labs(x="Date",y="Number of Observations", title = "Number of observations pre and post GDPR", col="#795fc6")+
  geom_vline(xintercept = as.numeric(DatasetApp$analysis_date[GDPR_date]), color=c("#5034c4"))+
  theme_minimal()

【问题讨论】:

  • 欢迎来到 SO!为了帮助我们帮助您,您能否通过分享您的数据样本来重现您的问题?见how to make a minimal reproducible example。只需在控制台中输入dput(NAME_OF_DATASET) 并将以structure(.... 开头的输出复制并粘贴到您的帖子中。如果您的数据集有很多观察结果,您可以对前 20 行数据执行例如dput(head(NAME_OF_DATASET, 20))

标签: r ggplot2 colors


【解决方案1】:

简而言之,您希望情节完全是紫色的。
可以使用 aes 参数之外的 fill 参数更改区域填充。文本颜色在theme 函数内设置,参数text(更改标题和轴标题的颜色)和axis.text(更改轴标签)。

数据

df <- data.frame(date = as.Date(c("2017-11-12", "2018-03-28",
                                  "2018-06-30", "2018-06-22", 
                                  "2018-09-24" ,"2018-11-21"), 
                                format = "%Y-%m-%d"))

代码

ggplot(data = df, aes(x=date)) +
  geom_area(stat="bin", fill = "#795fc6") +
  labs(x="Date",
       y="Number of Observations", 
       title = "Number of observations pre and post GDPR")+
  geom_vline(xintercept = df[3,1], 
             color="#5034c4")+
  theme_minimal() +
  theme(text = element_text(color = "#795fc6"),
        axis.text = element_text(color = "#795fc6"))

剧情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多