使用 ggplot 在绘图图中散点的双重注释
Dual annotations for scatter point in plotly graph using ggplot
我设计了一个闪亮的应用程序来绘制两个绘图。
其中一个图表是散点图,选中的点会通过改变颜色和大小来突出显示。我的代码在这里:
output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)
# Select the data for the chosen compartment using a switch statement.
# For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
subject_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)
g <- ggplot(Kcl_V %>% slice(-subject_id), aes(x = Vd, y = Cl)) + # Initialize ggplot object
geom_point(colour = "#F8766D",size = 3)+
geom_point(data = Kcl_V[subject_id, ],aes(x = Vd, y= Cl), colour = "#00BFC4", size = 4)
p <- ggplotly(g) # Convert to a plotly object.
# Doesn't create legend, but when hover over, does give label (which has to be the column name).
})
信息已正确绘制,但是,所选点始终具有这样的双重注释:
有人知道如何避免这种情况吗?谢谢!
发生这种情况是因为您在 ggplot()
和第二次调用 geom_point()
中都设置了 aes
,因此对于第二次 geom_point
中的所有数据,您有 2 组美学映射,导致 2 组工具提示。
有两种方法可以解决这个问题。首先,您可以删除 ggplot
中的 aes
集,而是分别在每个 geom_point
中设置它,一个数据集和美学映射用于选定的点,一个用于非选定的点。
output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)
# Select the data for the chosen compartment using a switch statement.
# For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
subject_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)
g <- ggplot() + # Initialize ggplot object
geom_point(data = Kcl_V[-subject_id, ],aes(x = Vd, y= Cl),colour = "#F8766D",size = 3)+
geom_point(data = Kcl_V[subject_id, ],aes(x = Vd, y= Cl), colour = "#00BFC4", size = 4)
p <- ggplotly(g) # Convert to a plotly object.
# Doesn't create legend, but when hover over, does give label (which has to be the column name).
})
或者,您可以在 Kcl_V 中创建一个布尔变量,指示该行是否属于所选点,并使用此变量设置颜色和大小美学映射。
output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)
# Select the data for the chosen compartment using a switch statement.
# For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
selected_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)
g <- ggplot(Kcl_V %>% dplyr::mutate(selected = subject_id == selected_id),aes(x = Vd, y= Cl,color = selected, size = selected)) + # Initialize ggplot object
scale_color_manual(values = c("TRUE"="#00BFC4","FALSE"="#F8766D")) +
scale_size_manual(values = c("TRUE"=4,"FALSE"=3))
p <- ggplotly(g) # Convert to a plotly object.
# Doesn't create legend, but when hover over, does give label (which has to be the column name).
})
我设计了一个闪亮的应用程序来绘制两个绘图。
其中一个图表是散点图,选中的点会通过改变颜色和大小来突出显示。我的代码在这里:
output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)
# Select the data for the chosen compartment using a switch statement.
# For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
subject_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)
g <- ggplot(Kcl_V %>% slice(-subject_id), aes(x = Vd, y = Cl)) + # Initialize ggplot object
geom_point(colour = "#F8766D",size = 3)+
geom_point(data = Kcl_V[subject_id, ],aes(x = Vd, y= Cl), colour = "#00BFC4", size = 4)
p <- ggplotly(g) # Convert to a plotly object.
# Doesn't create legend, but when hover over, does give label (which has to be the column name).
})
信息已正确绘制,但是,所选点始终具有这样的双重注释:
有人知道如何避免这种情况吗?谢谢!
发生这种情况是因为您在 ggplot()
和第二次调用 geom_point()
中都设置了 aes
,因此对于第二次 geom_point
中的所有数据,您有 2 组美学映射,导致 2 组工具提示。
有两种方法可以解决这个问题。首先,您可以删除 ggplot
中的 aes
集,而是分别在每个 geom_point
中设置它,一个数据集和美学映射用于选定的点,一个用于非选定的点。
output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)
# Select the data for the chosen compartment using a switch statement.
# For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
subject_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)
g <- ggplot() + # Initialize ggplot object
geom_point(data = Kcl_V[-subject_id, ],aes(x = Vd, y= Cl),colour = "#F8766D",size = 3)+
geom_point(data = Kcl_V[subject_id, ],aes(x = Vd, y= Cl), colour = "#00BFC4", size = 4)
p <- ggplotly(g) # Convert to a plotly object.
# Doesn't create legend, but when hover over, does give label (which has to be the column name).
})
或者,您可以在 Kcl_V 中创建一个布尔变量,指示该行是否属于所选点,并使用此变量设置颜色和大小美学映射。
output$ID <- renderPlotly({ # This render command makes everything inside the {} reactive (will update with changes to user input via widgets)
# Select the data for the chosen compartment using a switch statement.
# For a given input in the drop-down menu, it assigns the appropriate data frame to df to be plotted.
selected_id <- switch(input$ID,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5)
g <- ggplot(Kcl_V %>% dplyr::mutate(selected = subject_id == selected_id),aes(x = Vd, y= Cl,color = selected, size = selected)) + # Initialize ggplot object
scale_color_manual(values = c("TRUE"="#00BFC4","FALSE"="#F8766D")) +
scale_size_manual(values = c("TRUE"=4,"FALSE"=3))
p <- ggplotly(g) # Convert to a plotly object.
# Doesn't create legend, but when hover over, does give label (which has to be the column name).
})