library(ggplot2)
library(plotly)
gg <- ggplot(mtcars, aes(factor(vs), drat)) +
geom_violin() +
geom_jitter()
ggplotly(gg)
Run Code Online (Sandbox Code Playgroud)
在示例代码中,我们用于ggplot绘制小提琴和抖动层。Plotly显示两层的信息(即,当悬停在抖动点上时,它将显示特定的点信息,当悬停在小提琴图上时也会发生相同的情况)。但是,我只想plotly显示信息geom_jitter。
问题:如何禁用特定层的悬停信息?
这是提取点击事件的工作示例。我想问你是否有办法通过增加大小或突出显示等来更新点击点?
library(shiny)
library(plotly)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
nms <- row.names(mtcars)
output$plot <- renderPlotly({
p <- ggplot(mtcars, aes(x = mpg, y = wt, col = as.factor(cyl), key = nms)) +
geom_point()
ggplotly(p)
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)"
else cat("Selected point associated with Car: ", d$key)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我已经搜索了 SO 和其他合适的来源以找到以下问题的解决方案,但找不到。
更新:
已在此处询问有关更改单击点形状的相关问题。
我想Species在光标悬停在数据点上时显示每个数据点,而不是x和y值。我使用iris数据集。另外,我希望能够单击一个数据点以使标签保持不变,并且在我选择图中的新位置时不会消失。(如果可能的话 )。基本是标签。持久性问题是一个加号。这是我的应用程序:
## Note: extrafont is a bit finnicky on Windows,
## so be sure to execute the code in the order
## provided, or else ggplot won't find the font
# Use this to acquire additional fonts not found in R
install.packages("extrafont");library(extrafont)
# Warning: if not specified in font_import, it will
# take a bit of time to get all fonts
font_import(pattern = "calibri")
loadfonts(device = "win")
#ui.r
library(shiny)
library(ggplot2)
library(plotly)
library(extrafont)
library(ggrepel)
fluidPage(
# …Run Code Online (Sandbox Code Playgroud) 我有一个如上图所示的图表。可重用代码如下。
plotly::ggplotly(ggplot(dplyr::as_tibble(rnorm(1000)), aes(value)) + stat_ecdf(geom = 'line'))
Run Code Online (Sandbox Code Playgroud)
我想在悬停时重命名和格式化工具提示。例如,x 轴或“价值”(在图表中)可以是“单位价格?” 而 y 轴是累积分布。
因此,当我将鼠标悬停在线上时,我希望能够看到如下所示的内容
累积分布:78.2%
单价:?0.81
谢谢!
我想反转水平条形图图例的顺序。添加guides(fill = guide_legend(reverse = TRUE))到它时ggplot效果很好(参见第二个图)。但是,应用图例后,ggplotly()图例又恢复为默认顺序。
如何在不plotly改变条形顺序的情况下反转图例的顺序?
library(ggplot2)
library(dplyr)
data(mtcars)
p1 <- mtcars %>%
count(cyl, am) %>%
mutate(cyl = factor(cyl), am = factor(am)) %>%
ggplot(aes(cyl, n, fill = am)) +
geom_col(position = "dodge") +
coord_flip()
p1
Run Code Online (Sandbox Code Playgroud)

p2 <- p1 + guides(fill = guide_legend(reverse = TRUE))
p2
Run Code Online (Sandbox Code Playgroud)

plotly::ggplotly(p2)
Run Code Online (Sandbox Code Playgroud)
默认情况下,Plotly 在图例部分中单击其标签时隐藏该线。是否有任何其他键的组合shift/ctrl/alt/可以仅显示选定的行并隐藏所有其他行?
我想更改下图中单击点的形状和大小。如何实现?对于这个玩具图,我将点数从原来的 100k 减少到 2k。因此,预期的解决方案应该是高度可扩展的,并且不偏离原始图,即点击点更新之前和之后的所有颜色应该相同。
library(shiny)
library(plotly)
df <- data.frame(X=runif(2000,0,2), Y=runif(2000,0,20),
Type=c(rep(c('Type1','Type2'),600),
rep(c('Type3','Type4'),400)),
Val=sample(LETTERS,2000,replace=TRUE))
# table(df$Type, df$Val)
ui <- fluidPage(
title = 'Select experiment',
sidebarLayout(
sidebarPanel(
checkboxGroupInput("SelType", "Select Types to plot:",
choices = unique(df$Type),
selected = NA)
),
mainPanel(
plotlyOutput("plot", width = "400px"),
verbatimTextOutput("click")
)
)
)
Run Code Online (Sandbox Code Playgroud)
server <- function(input, output, session) {
output$plot <- renderPlotly({
if(length(input$SelType) != 0){
df <- subset(df, Type %in% input$SelType)
p <- ggplot(df, aes(X, Y, col = as.factor(Val))) +
geom_point()
}else{
p <- ggplot(df, aes(X, Y, …Run Code Online (Sandbox Code Playgroud) 好奇如何使用 ggplot 或 plotly 库函数绘制这个点图。还要在各个点上标记 mpg 值。
# Dotplot: Grouped Sorted and Colored
# Sort by mpg, group and color by cylinder
x <- mtcars[order(mtcars$mpg),] # sort by mpg
x$cyl <- factor(x$cyl) # it must be a factor
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
main="Gas Milage for Car Models\ngrouped by cylinder",
xlab="Miles Per Gallon", gcolor="black", color=x$color)
Run Code Online (Sandbox Code Playgroud)
我正在为 r 中的作业创建动画绘图图,其中我将多个模型与不同数量的观察结果进行比较。我想添加注释,显示当前模型的 RMSE 是多少 - 这意味着我希望文本与滑块一起变化。有什么简单的方法可以做到这一点吗?
这是我存储在 GitHub 上的数据集。已经创建了 RMSE 变量:data
基本 ggplot 图形如下:
library(tidyverse)
library(plotly)
p <- ggplot(values_predictions, aes(x = x)) +
geom_line(aes(y = preds_BLR, frame = n, colour = "BLR")) +
geom_line(aes(y = preds_RLS, frame = n, colour = "RLS")) +
geom_point(aes(x = x, y = target, frame = n, colour = "target"), alpha = 0.3) +
geom_line(aes(x = x, y = sin(2 * pi * x), colour = "sin(2*pi*x)"), alpha = 0.3) +
ggtitle("Comparison of …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种解决方法来获得我们使用 grid.arrange 使用 ggplotly 获得的图的布局。
grid.draw 生成的对象显然不是 ggplot 对象。因此,想知道将以下图传递给 ggplotly 的任何解决方法。
library(ggplot2)
library(gridExtra)
p1 = qplot(1:20,1:20)
p2 = qplot(1:20, 20:1)
grid.arrange(p1,p2, ncol=2)
Run Code Online (Sandbox Code Playgroud) 我的闪亮应用程序中有一个名为 p2 的 ggplotly 对象。我希望它能够工作,如果用户在应用程序 UI 中按下 downloadButton,它将把 ggplotly 图下载为 html 文件。但该功能不起作用:
output$Export_Graph <- downloadHandler(
filename = function() {
file <- "Graph"
},
content = function(file) {
write(p2, file)
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我想将我的数据拟合到已使用 Matlab 优化的特定函数中。
我收到以下错误:“警告消息:计算失败stat_smooth():奇异梯度”
请帮忙!这是我的 R 代码:
tibble
x y SEM
1 1 0.0342 0.00532
2 3 0.0502 0.00639
3 5 0.0700 0.0118
4 10 0.123 0.0269
5 20 0.154 0.0125
6 30 0.203 0.0190
7 40 0.257 0.0255
8 50 0.287 0.0266
9 60 0.345 0.0347
10 90 0.442 0.0398
11 120 0.569 0.0570
12 180 0.726 0.0406
13 240 0.824 0.0150
14 360 0.868 0.00821
15 1440 0.890 0.0246
tibble %>%
ggplot(aes(x, y)) +
geom_point()+
geom_errorbar(aes(ymin=y-SEM, …Run Code Online (Sandbox Code Playgroud)