I have a bar chart that I want to make interactive in my R Shiny app using plotly.
When it is rendered as a plot, everything is fine using ggplot:
#Bar plots
ggplot(data = df_continents) +
geom_bar(aes(x=country, y=coal_co2), stat="identity", fill="#2596be") +
scale_y_continuous(labels = number_format())`
But, when I try to use plotly, the format gets messed up in an incredible way:
#Bar chart - Adding Labels
ticklabels <- seq(from=0, to=round(max(df$coal_co2*100000)), by=100000)
ticktexts <- c(0,paste(ticklabels[-1]/1000, " 000", sep=""))
output$bar <- renderPlotly({
df %>%
plot_ly(x =~ country, y = ~ coal_co2, type = "bar", marker = list(color = "#2596be")) %>%
layout(yaxis=list(tickvals = ticklabels,
ticktext = ticktexts
))
})
So, I'm not sure what is the problem here, why do I have those white horizontal lines inside the bars?. How do I get my bar chart plot to look like the first screenshot?.
Related
I am trying to hide the plotly toolbar when displaying a reactive ggplot chart within a Shiny app. Currently my code looks like this:
renderPlotly( ggplot(users_activated(), aes(x = month, y = n)) +
geom_bar(stat = "identity"))
I have tried config(displayModeBar = F) to no avail.
You need to convert your ggplot object to a plotly object before you can render it as one, or use plotly functions on it. ggplotly does that. Below, is an example using mpg dataset.
ggplotly(ggplot(mpg, aes(class, displ, color = manufacturer)) +
geom_point()
) %>%
config(displayModeBar = F)
I am trying to plot a gantt chart and have created it successfully, but being too many values involved, it is visually not presentable.
What should i add to the below code to add a vertical scroll bar in the chart. Also, how can i assign customized colors to the bar & change the background & font.
df <- read.csv("C:/Users/XXXX/Downloads/XXXX.csv" , stringsAsFactors = F)
df$Timeline.Start <- as.Date(df$Timeline.Start, format = "%m/%d/%Y")
df$Timeline.End <- as.Date(df$Timeline.End, format = "%m/%d/%Y")
df$System.Lease.ID <- as.character(df$System.Lease.ID)
ggplot(df, aes(colour= Timeline.Type, group = System.Lease.ID)) +
geom_segment(aes(x=Timeline.Start, xend=Timeline.End, y=System.Lease.ID, yend=System.Lease.ID), size=3) +
labs(x="Timeline.Start", y="System.Lease.ID", title="Project timeline")
I want to plot a facet plot with added geom_hline and show the line in the legend. However, when I add the line to the legend, all entries duplicate to the number of facets.
How can I avoid this behaviour?
Here is my MWE
library(shiny)
library(plotly)
library(ggplot2)
df <- mpg
# Define UI for application that draws a histogram
ui <- fluidPage(
mainPanel(
plotlyOutput('graph')
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$graph <- renderPlotly({
p <- ggplot2::mpg %>%
plot_ly %>%
ggplot() +
geom_point(aes(displ, hwy, color = class))
facet <- p + facet_wrap(~year)
facet + geom_hline(aes(yintercept = 20, linetype = 'hline20'), color = '#00b700') +
scale_linetype_manual(name = 'line', values = 1,
guide = guide_legend(aes = list(color = "#00b700")))
})
}
# Run the application
shinyApp(ui = ui, server = server)
The problem is not shiny nor ggplot2. The problem is the conversion of a ggplot2 object into a plotly object.
Below I've isolated the ggplot steps from the plotly step and completely removed shiny from the picture.
p <- ggplot2::mpg %>%
ggplot() +
geom_point(aes(x = displ, y = hwy, color = class))
facet <- p + facet_wrap(~year)
final <- facet + geom_hline(aes(yintercept = 20, linetype = 'hline20'), color = '#00b700') +
scale_linetype_manual(name = 'line', values = 1, guide = guide_legend(aes = list(color = "#00b700")))
print(final)
The above displays fine.
ggplotly(final)
This above has the same display errors as yours, without involving shiny.
Legend issues and other display issues are common when converting ggplot2 objects to plotly objects. Plotly and ggplot2 are completely independent plotting frameworks with their own syntax and graphics objects. Plotly provides conversion function methods but things still get lost in translation from one format to the other, because there is not a complete equivalency between them.
I'd recommend trying to implement your plot in native plotly syntax instead of trying to convert it from ggplot2.
thank you in advance for the help.
I am trying to use plotly within a shiny app I have. I am a bit new to plotly so I apologize if this is a no brainer. I would like to use a scatterplot that paints each point a different color based on input$Col, then for each group within input$Col I would like to draw a smoothed line for each color. Everything works fine except for the fact that when I use add_lines() it just draws one line, and does not differentiate for the different colors that are on the scatterplot.
plot_ly(poolfinderdata1(), type = "scatter", x = ~get(input$X),
y = ~get(input$Y),
mode = "markers",
color = ~get(input$Col),
symbol = as.factor(poolfinderdata1()$Matcher))%>%
add_lines(y = ~fitted(loess(get(input$Y) ~ get(input$X)),
color ~ get(input$Col)))%>%
layout(xaxis =list(title= input$X), yaxis = list(title = input$Y))
ggplot2 has been around for a long time so it already has a bunch of stuff you can use and I think learning it it's worth the effort, specially since you can turn your plots interactive with the ggplotly function. Here's one way to solve your problem:
library(plotly)
library(ggplot2)
data <- poolfinderdata1()
data[[input$Col]] <- as.factor(data[[input$Col]]) # so the color is mapped as discrete
data$Matcher <- as.factor(data$Matcher) # same for the markers
p <- ggplot(data, aes_string(x = input$X, y = input$Y, color = input$Col)) +
geom_point(aes(shape = Matcher)) +
geom_smooth(method = "loess")
ggplotly(p)
Hope this helps.
I just installed the latest plotly R package (devtools::install_github("ropensci/plotly")).
I'm trying to generate a violin plot for a single variable and I would like to suppress the x-axis label.
I tried:
library(dplyr)
library(plotly)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")
plot_ly(y =~ df$total_bill,type = 'violin',
box = list(visible = T),
meanline = list(visible = T)) %>%
layout(xaxis = list(title = ""),yaxis = list(title = "Total Bill",zeroline = F))
But I'm getting "trace 0" as the x-axis label:
I tried playing around with the x0 parameter but couldn't get a violin with no x-axis label.
Any idea?
Here's an example using ggplot2 + ggplotly:
library(dplyr)
library(plotly)
librart(ggplot2)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")
# Generate a ggplot with a violin plot and a boxplot
vioplot <- ggplot(df, aes(x = "", y=total_bill)) +
geom_violin(fill = "blue", alpha = 0.3) +
geom_boxplot(fill = "blue", alpha = 0.3) +
xlab("") + ylab("Total bill")
# Render the ggplot as a plotly object using ggplotly()
ggplotly(vioplot)
Here is the resulting plotly
P.S. I wan't able to reproduce your plot using plot_ly(). I Got an error "Error: Trace type must be one of the following:...". Anyway I'm not a big fan of the plot_ly() way. I always go first through a ggplot.