r - 使用 ggplot2 更改 barplot 条内的颜色
问题描述
我有以下条形图,但我只能更改边框的颜色。无论我尝试什么,我都无法更改条形图中的颜色,它仍然是灰色的。
pteddf=data.frame(x=c(0:12),runif(13),runif(13))
tidyr::pivot_longer(pteddf, -x)
colors=c("red", "blue")
ggplot(tidyr::pivot_longer(pteddf, -x),aes(-x,value,color=name,group=name))+
geom_bar(stat="identity",position=position_dodge())+
scale_color_manual(name="",labels = c("Negative TED","Positive TED"),
values= colors)+
scale_fill_manual( values = colors)
解决方案
尝试这个。您可以fill
在该aes()
部分中使用来更改填充的颜色:
pteddf=data.frame(x=c(0:12),runif(13),runif(13))
tidyr::pivot_longer(pteddf, -x)
colors=c("red", "blue")
ggplot(tidyr::pivot_longer(pteddf, -x),aes(-x,value,
fill=name,
color=name,group=name))+
geom_bar(stat="identity",position=position_dodge())+
scale_color_manual(name="",labels = c("Negative TED","Positive TED"),
values= colors)+
scale_fill_manual(name="",values = colors,
labels = c("Negative TED","Positive TED"))
输出:
推荐阅读
- scala - 布尔返回问题scala
- selenium - 如何解决 IE Webdriver 服务器初始启动页面错误 - Selenium IE Webdriver with JMeter
- c# - EF Core Lambda Expression Object Reference not set to an instance of an object on multiple joins
- kotlin - 带索引的流上的 ForEach
- python - 如何从 np.datetime64 中提取小时/分钟/秒
- azure-functions - 如何通过记录删除清除持久实体历史记录
- r - 在 R 循环中读取具有相同扩展名的每个文件的通配符
- hive - Hive - 从三个外部表创建一个内部表
- sql - SSIS 的 Konesan 校验和不在工具箱中
- python - 比较两个文本文件并将匹配值写入文本文件