是否可以将 ggplot ly和 ggplot 与拼凑而成?
这将并排显示两个图
library(ggplot2)
library(plotly)
library(patchwork)
a <- ggplot(data.frame(1), aes(X1)) + geom_bar()
b <- ggplot(data.frame(1), aes(X1)) + geom_bar()
a + b
Run Code Online (Sandbox Code Playgroud)
但是,如果我们将其转换为 ggplotly,则会出错
b <- ggplotly(b)
a + b
Error: Can't add `b` to a ggplot object.
Run `rlang::last_error()` to see where the error occurred.
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题?
使用样本数据:
library(tidyverse)
library(plotly)
myplot <- diamonds %>% ggplot(aes(clarity, price)) +
geom_boxplot() +
facet_wrap(~ clarity, ncol = 8, scales = "free", strip.position = "bottom") +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank())
ggplotly(myplot)
Run Code Online (Sandbox Code Playgroud)
返回类似:
与第一个和最后一个相比,内部刻面的缩放非常糟糕,并且有很多额外的填充。我试图从这些问题中找到解决方案:
R:facet_wrap 无法在 Shiny 应用程序中使用 ggplotly 正确呈现
通过反复试验,我使用panel.spacing.x = unit(-0.5, "line")
了theme()
它,它看起来更好一些,很多额外的填充消失了,但内部方面仍然明显更小。
同样作为一个额外的问题,但不是那么重要ggplotly()
,当我将它们设置在底部时,条形标签是调用中的顶部。这里似乎是一个持续存在的问题,有没有人有一个hacky的解决方法?
编辑:在我的真实数据集中,我需要每个方面的 y 轴标签,因为它们的比例非常不同,所以我将它们保留在示例中,这就是为什么我需要facet_wrap
. 我的真实数据集的屏幕截图以供解释:
我有以下数据集:
dput(head(active_clients))
structure(list(Date = structure(c(1422662400, 1425081600, 1427760000,
1430352000, 1433030400, 1435622400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), value = c(65139, 66615, 66669, 67081, 67277,
67366), month = 1:6, year = c(2015L, 2015L, 2015L, 2015L, 2015L,
2015L), year_month = c("1/15", "2/15", "3/15", "4/15", "5/15",
"6/15"), year2 = c("15", "15", "15", "15", "15", "15")), .Names = c("Date",
"value", "month", "year", "year_month", "year2"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
我正在使用ggplot2绘制以下线/点图.
t <- ggplot(active_clients) +
geom_point(aes(as.factor(year_month),
value),
size = 2, …
Run Code Online (Sandbox Code Playgroud) 我有一个我用 plotly 创建的情节。当我将它部署到我闪亮的应用程序时,X 和 Y 标签被切断,如下所示:
我怎样才能防止这种情况发生?如果我使用普通图,标签不会被切断,但我需要图是交互式的。
这是我创建情节的代码:
ui.r:
#creating app with shiny
library(shiny)
library(shinydashboard)
shinyUI(
dashboardPage(
dashboardHeader(title = "Dashboard"),
dashboardSidebar(
menuItem("Dashboard")
),
dashboardBody(
fluidPage(
box(plotlyOutput("bakePlot")),
box(plotOutput("bakeMonthly"))
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
服务器.r:
shinyServer(function(input, output){
output$bakePlot <- renderPlotly({
ggplot(sales_bakery, aes(ProductName, ProductSales))+
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = FALSE) +
coord_cartesian(ylim = c(7000, 9500)) + ggtitle("January Sales in Bakery") +
xlab("Category") + ylab("Quantity Sold")+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(angle = 60, hjust = 1),
axis.text.y = element_text(colour = "black", size = …
Run Code Online (Sandbox Code Playgroud) 我想让我的散点图点可点击,并在点击时打开每个点各自的超链接。我正在尝试使用 ggplotly 来做到这一点。使用 plotly() 构建绘图时,有一种简单的方法可以做到这一点(参见下面的第一个示例)。但是,在 ggplotly 上运行它时,单击时会打开一个空网页。
任何人都知道我如何使用下面的代码进行这项工作?代码的 OnRender 部分是否有解决方案(不熟悉 javascript)?使用 plotly 的第一个代码块有效。第二个代码块是我使用 ggplotly 的基本尝试,但它不起作用。
任何关于这方面的指导都会很棒!提前致谢
library(plotly)
library(htmlwidgets)
library(dplyr)
#1. Using plotly
mtcars$url <- paste0(
"http://google.com/#q=",
rownames(mtcars))
p <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
add_markers(customdata = ~url)
onRender(p, "
function(el, x) {
el.on('plotly_click', function(d) {
var url = d.points[0].customdata;
window.open(url);
});
}
")
#2. Using ggplotly`
p <- ggplot(data = mtcars, aes(x = wt, y = mpg))+
geom_point()
pp <- ggplotly(p)
pp <- add_markers(pp, customdata = ~url) …
Run Code Online (Sandbox Code Playgroud) 我有下面的数据框:
Val1<-c(0.5,0.7,0.8,0.9)
Val2<-c(0.5,0.7,0.8,0.9)
Val3<-c(0.5,0.7,0.8,0.9)
Val4<-c(0.5,0.7,0.8,0.9)
vales<-data.frame(Val1,Val2,Val3,Val4)
row.names(vales)<-c("asd","dasd","dfsdf","fdff")
Run Code Online (Sandbox Code Playgroud)
我对其进行了正确处理,以便使用以下命令创建聚类散点图:
library(tidyverse) # data manipulation
library(cluster) # clustering algorithms
library(factoextra) # clustering algorithms & visualization
library(plotly)
cl<-scale(vales)
dist <- get_dist(cl)
k2 <- kmeans(cl, centers = 2, nstart = 25)
cl %>%
as_tibble() %>%
mutate(cluster = k2$cluster,
state = row.names(vales))
p2<-fviz_cluster(k2, data = cl)
p2+geom_text(aes(label=""))
#or
ggplotly(p2+geom_text(aes(label="")))
Run Code Online (Sandbox Code Playgroud)
我想删除点的标签,但我不明白为什么它们出现,而在下面的情况下却没有出现。
df <- USArrests
df <- na.omit(df)
df <- scale(df)
distance <- get_dist(df)
k2 <- kmeans(df, centers = 2, nstart = 25)
df %>%
as_tibble() %>%
mutate(cluster …
Run Code Online (Sandbox Code Playgroud) 我使用 ggplotly 在 R 中创建了一个地图。要创建链接,其大小需要为 524kb 或以下,但目前为 1.2Mb。有什么好的方法可以减少文件大小以便我可以导出吗?或者这完全不现实?
我正在尝试在地图上累计绘制每个月打开的新位置。我可以每月创建一个包含新位置的动画,但不能累积。换句话说,我希望看到新地点添加到现有地点。
这是示例数据
DF <- data.frame("latitude" = c(42.29813,41.83280,41.83280,30.24354),
"longitude" =c(-71.23154,-72.72642,-72.72642,-81.62098),
"month" = c(1,2,3,4))
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的
usa <- ggplot() +
borders("usa", colour = "gray85", fill = "gray80") +
theme_map()
map <- usa +
geom_point(aes(x = longitude, y = latitude, cumulative=TRUE,
frame=month,stat = 'identity' ),data = DF )
map
# Generate the Visual and a HTML output
ggp <- ggplotly(map)%>%
animation_opts(transition = 0)
ggp
Run Code Online (Sandbox Code Playgroud)
输出不累计显示位置。基本上我想最后看到所有四个地点。
我使用 ggplot2 使用以下代码绘制了以下两个图表。
p <- ggplot() +
geom_line(data = campaigns_profit_3months, aes(y = clients_3monthsBefore, x = c(1:41), group = 1, color = "Clients 3 Months Before"),
size = 1 ) +
geom_line(data = campaigns_profit_3months, aes(y = clients_3monthsAfter, x = c(1:41), group = 1, color = "Clients 3 Months After"),
size = 1 ) +
xlab('Campaigns') + ylab('Clients') + ggtitle('Clients 3 months before and after the campaigns') +
scale_x_continuous(breaks = seq(1,41,1)) + scale_y_continuous(limits=c(0, 200)) +
theme(legend.title=element_blank())
q <- ggplot() +
geom_line(data = …
Run Code Online (Sandbox Code Playgroud) 总体思路是创建一个可以在显示计数和百分比之间切换的绘图图表。我可以使用更新菜单更改显示的迹线,并且轴刻度可以动态更改,但是当我在下面的示例中切换到“%”时,图例消失。
我根本不是一个情节专业人士,这就是我使用的原因ggplotly()
(我的真实示例更复杂并且ggplolty()
效果很好!)并且我想知道是否必须手动将图例添加到 % 迹线 (3, 4) 中当第一个痕迹不可见时是否显示图例?
library(ggplot2)
library(plotly)
df <- structure(list(outcome = c("a", "b", "a", "b", "a", "b", "a",
"b", "a", "b"), n = c(59, 191, 28, 67, 29, 56, 33, 45, 32, 40
), pct = c(0.208480565371025, 0.674911660777385, 0.288659793814433,
0.690721649484536, 0.337209302325581, 0.651162790697674, 0.4125,
0.5625, 0.444444444444444, 0.555555555555556), day = c(1L, 1L,
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L)), class = "data.frame",
row.names = c(NA, -10L))
p <- ggplot(df, aes(day, n, color = outcome)) + …
Run Code Online (Sandbox Code Playgroud) ggplotly ×10
r ×10
ggplot2 ×7
plotly ×6
r-plotly ×4
factoextra ×1
filesize ×1
htmlwidgets ×1
patchwork ×1
shiny ×1