r - 在闪亮的条形图中更改条形的颜色
问题描述
我有一个闪亮的应用程序,它基本上根据用户在表格中选择的线条生成一个图表。有两个表:第一个生成左侧的前 3 个柱(基准),其他来自第二个表。
我对您的问题是:您认为,是否可以将左侧的前 3 个条保留为蓝色,而将其他颜色用于所有其他条?
这是我在应用程序中用于生成图表的代码:
output$graphPost <- renderPlot({
s <- input$posttestsdata_rows_selected
y <- input$benchmarkdata_rows_selected
tempBench <- benchmarkData[y]
meltedBench <- melt(tempBench)
tempPost <- postTestsData[s]
colnames(tempBench)[1] <- "x"
colnames(tempPost)[1] <- "x"
postTestsDataForGraph <- rbind(tempBench, tempPost)
meltPostTests <- melt(postTestsDataForGraph)
meltPostTests$x <- factor(meltPostTests$x, levels=unique(meltPostTests$x))
postTestsPlot <<- ggplot() +
geom_bar(data = meltPostTests, aes(x = as.factor(x), y = value, fill = variable), stat='identity', position = "dodge") +
theme(axis.line=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank()) +
geom_hline(yintercept = meltedBench$value, color = c("#1F497D", "#4F81BD", "#8DB4E3")) +
geom_text(aes(x = as.factor(meltPostTests$x), y = meltPostTests$value, fill=meltPostTests$variable, label = paste(meltPostTests$value,"%", sep = "")), position=position_dodge(width=0.9), vjust=-0.25) +
scale_fill_manual(values = c("#1F497D", "#4F81BD", "#8DB4E3"))
return(postTestsPlot)
})
谢谢,
雷米
解决方案
要影响前 3 个条的颜色,您需要使用另一个美学值,而fill
不是variable
例如:
require(ggplot2)
set.seed(314)
dat <- data.frame(x = rep(1:3,10),
variable = sample(1:3, 30, replace = T))
dat$c <- as.factor(ifelse(dat$x == 1,1,dat$variable+1))
ggplot(dat, aes(x = interaction(variable,x), fill = c)) +
geom_bar(data = dat, aes(fill = c), position = position_dodge()) +
scale_x_discrete(breaks = c('2.1','2.2','2.3'),
labels = unique(dat$variable))
给出:
推荐阅读
- sql - INNER JOIN 多个数据库表中的 SQL 计数
- javascript - 如何删除 Qualtrics 中滑块问题左侧的选择文本?
- java - 来自 Vertx 网络客户端响应正文的反应式流发布者
- visual-studio - 如何使用不同的 SQL 数据库处理多个客户
- python - 十二生肖日历样本中日期的意外输入值检测
- ansible - 如何在同一环境中为多个服务器/主机运行 Playbook
- node.js - 发送超过 8KB 的响应数据时出现问题
- wordpress - 网站在 wordpre 请求上返回错误
- javascript - 每次按下下一个问题按钮时,我都想将其更改为数组中写入的内容
- laravel - 调用数组 Laravel 上的成员函数 paginate()