我有一个数据框df。
df <- structure(list(Mills = c("Mill-A", "Mill-B", "Mill-C", "Mill-D",
"Mill-E"), Performance = c(0.5, 0.4, 0.2, 0.9, 0.4)), row.names = c(NA,
-5L), class = "data.frame")
df
Mills Performance
1 Mill-A 0.5
2 Mill-B 0.4
3 Mill-C 0.2
4 Mill-D 0.9
5 Mill-E 0.4
有没有办法在x轴上绘制Mill-A,在y轴上绘制性能。我尝试使用下面的代码,但没有得到。谁能帮我吗?
ggplot(data = df,aes(x=df$Mills[1],y=Performance, fill=Performance))+geom_bar(stat = "identity",width = 0.5)+theme(plot.margin = unit(c(1,1,1,1),"cm"))+theme(axis.title.x = element_blank())+theme(axis.title.y = element_blank())+theme(legend.position = "none")+theme(axis.text.x = element_text(angle = 0,vjust = 1,size = 5))+theme(axis.text.y = element_text(angle = 0,vjust = 1,size = 5))
原因是,我将其合并到R Shiny应用程序中,并且具有Mills过滤器(Mill-A,Mill-B等)。因此,对于每个过滤器,都应绘制出曲线图
我已经在R Shiny应用程序中尝试了以下代码,但无法正常工作
selectInput("Mill","Equipment",choices = c("All","Mill-A","Mill-B","Mill-C","Mill-D","Mill-E"),selected = "All",multiple = TRUE)
output$g34 <- renderPlot({
if (input$Mill != "All") {
req(input$Mill)
pd <- df %>% filter(Mills %in% input$Mill)
g34 <- ggplot(data = pd,aes(x=Mills,y=Performance, fill=Performance))+geom_bar(stat = "identity",width = 0.5)+theme(plot.margin = unit(c(1,1,1,1),"cm"))+theme(axis.title.x = element_blank())+theme(axis.title.y = element_blank())+theme(legend.position = "none")+theme(axis.text.x = element_text(angle = 0,vjust = 1,size = 5))+theme(axis.text.y = element_text(angle = 0,vjust = 1,size = 5))
}})
plotOutput("g34")
答案 0 :(得分:1)
尝试在绘制之前过滤数据。这将只绘制一个条形图,因此我假设有更多数据,这只是一个示例。
这里selected_mill
是您的Shiny应用中的input$
。
library(dplyr)
library(ggplot2)
df %>%
filter(Mills == selected_mill) %>%
ggplot(aes(x = Mills, y = Performance, fill = Performance) +
(the rest of your ggplot stuff here)
请记住,在函数末尾还要返回您的绘图对象!
output$g34 <- renderPlot({
if (input$Mill != "All") {
req(input$Mill)
pd <- df %>% filter(Mills %in% input$Mill)
g34 <- ggplot(plot things)
return(g34)
}})