我想把geom_col的边框变成透明的。当只使用ggplot2时,它可以工作。
library(ggplot2)
dataToPlot <- data.frame(Freq = c(0.0000000, 0.7092199, 1.4184397, 2.1276596, 2.8368794),
variable = rep('A',5), value = c(43089.76, 62923.17, 35446.15, 29553.76, 22433.08))
p <- ggplot( dataToPlot , aes(x=Freq, y = value, group = variable ) ) + #
# geom_bar(stat = "bin") fill = variable,
geom_col( mapping = aes(col = variable, fill = variable), colour = F, alpha = 0.2, orientation = "x", position = "dodge") +
# scale_linetype(aes(linetype = 0))
guides(color = FALSE)
dev.new(); p
但是,在使用shiny的情况下,同样的代码会出现以下错误 "错误:无效的颜色名称'FALSE'"
library(ggplot2)
library(shiny)
dataToPlot <- data.frame(Freq = c(0.0000000, 0.7092199, 1.4184397, 2.1276596, 2.8368794),
variable = rep('A',5), value = c(43089.76, 62923.17, 35446.15, 29553.76, 22433.08))
ui <- fluidPage(
useShinyjs(),
fluidRow(
column(8,
plotOutput("plot")
)
)
)
server <- function(input, output) {
output$plot <- renderPlotly({
p <- ggplot( dataToPlot , aes(x=Freq, y = value, group = variable ) ) + #
# geom_bar(stat = "bin") fill = variable,
geom_col( mapping = aes(col = variable, fill = variable), colour = F, alpha = 0.2, orientation = "x", position = "dodge") +
# scale_linetype(aes(linetype = 0))
guides(color = FALSE)
})
}
shinyApp(ui,server)
我到底做错了什么?
你的做法有些错误。
首先,你忘了说你也使用了包的 shinyjs
和 plotly
.
第二,你使用的是 renderPlotly
的服务器部分,但调用 plotOutput
中的ui。正确的是 plotlyOutput
中,因为你想要一个有情节的图形。
另一个问题是:既然你想要一个plotly类型的图形,你必须将你的ggplot图形转化为 p
到一个情节性的。因此,你应该添加 ggplotly(p)
到服务器部分。
最后,为了解决边框的问题,你应该使用 colour = NA
而不是 colour = FALSE
. 第二种方法在ggplot2中可以使用,但在plotly中却不行。我不知道具体原因。也许有人能澄清这个问题。
所以,你的代码应该是这样的。
library(ggplot2)
library(shiny)
library(shinyjs)
library(plotly)
dataToPlot <- data.frame(Freq = c(0.0000000, 0.7092199, 1.4184397, 2.1276596, 2.8368794),
variable = rep('A',5),
value = c(43089.76, 62923.17, 35446.15, 29553.76, 22433.08))
ui <- fluidPage(
useShinyjs(),
fluidRow(
column(8,
plotlyOutput("plot")
)
)
)
server <- function(input, output) {
output$plot <- renderPlotly({
p <- ggplot(dataToPlot , aes(x=Freq, y = value, group = variable)) +
geom_col(mapping = aes(col = variable, fill = variable), colour = NA, alpha = 0.2, orientation = "x", position = "dodge") +
guides(color = FALSE)
ggplotly(p)
})
}
shinyApp(ui,server)