简体   繁体   English

在 R 条 plot 中更改颜色

[英]change colour in an R bar plot

here is the structure of my dataset for reproducibility:这是我的数据集的可重复性结构:

 structure(list(type_de_sejour = c("Hospitalisé", "Hospitalisé", 
 "Hospitalisé", "Hospitalisé", "Hospitalisé", "Hospitalisé"
 ), site = c("FRA", "FRA", "FRA", "FRA", "FRA", "FRA"), specialite = c("ANESTHESIE 
 REANIMATION", 
 "Autres", "CARDIO VASCULAIRE", "CHIRUGIE CARDIAQUE", "CHIRURGIE GENERALE ET VISCERALE", 
  "CHIRURGIE THORACIQUE et VASCULAIRE"), proportion = c(0.000844059928254906, 
  0, 0.357986917071112, 0.0880987550116058, 0.00105507491031863, 
  0.0742772736864317), annee = c("2019", "2019", "2019", "2019", 
  "2019", "2019")), row.names = c(NA, 6L), class = "data.frame")

And here is my code for my plot.这是我的 plot 的代码。 It gives me a plot with rainbow colours so it's difficult to differentiate the different parts of my stacked bar chart (colour gradient).它给了我一个带有彩虹色的 plot,因此很难区分堆叠条形图(颜色渐变)的不同部分。 How can I change the colours, to make it both nice visually and easy to read?如何更改颜色,使其既美观又易于阅读?

ggplot(NCN_hosp, aes(x = annee, y = proportion, fill = specialite)) +
   geom_bar(stat = "identity") + 
   facet_wrap(~site) +
   theme(plot.title = element_text(hjust = 0.5, vjust = 1, size = 8),
         axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
         axis.text.y = element_text(size = 5), 
         legend.text = element_text(size = 5),
         legend.key.height = unit(0.1, "cm"), 
         panel.grid.major = element_blank(), 
         panel.grid.minor = element_blank())+
   scale_y_continuous(labels = scales::percent)

You're using (ggplot2).您正在使用(ggplot2)。 With this package Colors are defined by this variable, to include into plot code.有了这个 package Colors 由这个变量定义,包含到 plot 代码中。

     col=c("black", "red","blue","cyan")

As an example, this code generates a plot with several inputs and different Colors.例如,此代码生成具有多个输入和不同 Colors 的 plot。

plot(train_data["2019", 
                c("RV", "pred_HAR_RV","pred_EN_HAR_RV","pred_EN_HAR_RV_RIDGE")], 
     col=c("black", "red","blue","cyan"),
     lwd = c(1,1), 
     main = "Actual vs predicted RVs", 
     legend.loc = "topleft")

For those kind of questions, you should refer directly to the documentation of the package.对于这类问题,您应该直接参考 package 的文档。 Best.最好的。

you can either use this: scale_fill_manual(values=c("#199999","#269F00","#36B4E9","#A6B4E1","#51B4E1","#F69F10")) or this: scale_fill_manual(values=c("red","green","blue","yellow","orange","gray20","gray40"))你可以使用这个: scale_fill_manual(values=c("#199999","#269F00","#36B4E9","#A6B4E1","#51B4E1","#F69F10"))或者这个: scale_fill_manual(values=c("red","green","blue","yellow","orange","gray20","gray40"))

that is, you may use hex code or names, there are also packages if you search, but the colors you will use of course are subject of your choice.也就是说,您可以使用十六进制代码或名称,如果您搜索也有包,但是您将使用的 colors 当然是您选择的主题。 This page may give you some ideas of what colors to use https://r-graph-gallery.com/ggplot2-color.html这个页面可能会给你一些关于 colors 使用的想法

ggplot(NCN_hosp, aes(x = annee, y = proportion, fill = specialite)) +
  geom_bar(stat = "identity") + 
  facet_wrap(~site) +
  theme(plot.title = element_text(hjust = 0.5, vjust = 1, size = 8),
        axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
        axis.text.y = element_text(size = 5), 
        legend.text = element_text(size = 5),
        legend.key.height = unit(0.1, "cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+
  scale_y_continuous(labels = scales::percent)+
  scale_fill_manual(values=c("#199999","#269F00","#36B4E9","#A6B4E1","#51B4E1","#F69F10"))+
  scale_fill_manual(values=c("red","green","blue","yellow","orange","gray20","gray40"))

Of course, it depends on what you want, but you could also use standard colors palettes like scale_fill_brewer for example:当然,这取决于您想要什么,但您也可以使用标准 colors 调色板,例如scale_fill_brewer

library(ggplot2)
ggplot(NCN_hosp, aes(x = annee, y = proportion, fill = specialite)) +
  geom_bar(stat = "identity") + 
  scale_fill_brewer() +
  facet_wrap(~site) +
  theme(plot.title = element_text(hjust = 0.5, vjust = 1, size = 8),
        axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
        axis.text.y = element_text(size = 5), 
        legend.text = element_text(size = 5),
        legend.key.height = unit(0.1, "cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+
  scale_y_continuous(labels = scales::percent)

Please take a look at this site for some good option to change your colors of your bars.请查看此站点以获取一些不错的选项来更改您的 colors 吧。

Created on 2022-08-10 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 8 月 10 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM