Ich habe die folgende Funktion mit ggplot2
Plot. Ein Feature der Funktion ist der str_wrap
Funktionsaufruf, um die sehr langen Etiketten zu behandeln, die ich habe.R - Handle lange Etiketten in Plot
PlotResponseRate <- function(EntryData)
{
PlotData <- as.data.frame(apply(X = EntryData, MARGIN = 2,
function(x) round(length(which(!is.na(x)))/length(x)*100)))
colnames(PlotData) <- "TheData"
PlotData$TheLabel <- factor(str_wrap(colnames(EntryData), width = 30),
levels = unique(str_wrap(colnames(EntryData), width = 30)))
Graphe <- ggplot(data = PlotData, aes(x = TheLabel, y = TheData)) +
geom_bar(stat = "identity", fill = "red", width = 0.8) +
coord_flip() +
labs(title = "Response rate")
}
Es funktioniert in der „normalen“ R environnement in Ordnung und ich möchte es mit plotly
verwenden. Deshalb füge ich die folgende Codezeile:
PlotData$TheLabel <- gsub(pattern = "\n", replacement = "\n<br>", PlotData$TheLabel)
Es scheint zu funktionieren, aber es gibt noch ein Problem: der Raum, in dem links neben dem Grundstück zu breit ist (ich bin nicht sicher, aber es sieht aus wie es ist der gleiche Raum, den das lange Etikett tun würde).
Wie umgehen Sie dieses Verhalten?
Hier ist ein einfaches Beispiel:
library(stringr)
library(ggplot2)
library(plotly)
a <- c(1, 2, 2, 2, NA, 1, 2, 2, 1)
b <- c(2, 1, 2, NA, 2, NA, 1, NA, 1)
df <- data.frame(a, b)
colnames(df) <- c("This Is A Long Answer To A Long Question Label For The First Question",
"This Is A Long Answer To A Long Question Label For The Second Question")
TheGgplot2Plot <- PlotResponseRate(df)
ThePlotlyPlot <- ggplotly(TheGgplot2Plot)
print(ThePlotlyPlot)
Dank, es funktioniert in R, aber ich habe einen Fehler in Shiny 'Ausgang $ TheResponseRate <- renderPlotly ({ MyPlot <- PlotResponseRate (entrydata = df) MyPlot <- plotly_build (MyPlot) MyPlot $ x $ layout Marge $ $ l <- 180 }) ' – Kumpelka
Die Nachricht ist" Fehler in UseMethod: keine Methode für 'ggplotly' anwendbar auf ein Objekt der Klasse" c ('double', 'numeric') ". – Kumpelka