所以我有兩個數據框......其中一個是一個寬格式,列名是日期,日期下列出了那些日子發生的所有問題。第二個數據幀是一個有多種列描繪的事物等信息that've發生在那一天..就像這樣:使用數據框中的行值來選擇第二個數據框中的列
df_1 <- 2017-07-15 2017-08-15 2017-09-15 2017-10-15
'crashed' 'crashed' 'reset' 'crashed'
'damaged' 'reset' 'reset' 'reset'
'no problems' 'crashed' 'crashed' 'reset'
df_2 <- Date Make Model Color
2017-07-15 iPhone 7 black
2017-08-15 Android Galaxy silver
2017-09-15 iPhone 6 white
2017-10-15 Blackberry Curve black
我要訪問df_1的列誰的所有數據名稱與df_2的日期相匹配...原因是因爲我試圖使用來自df_2的信息作爲ggplot geom_point()
,並在光標懸停在同一日期的某個點上時在工具提示中顯示來自df_1的信息
(下面的代碼是從這裏取的,只是輸入我的數據幀): https://gitlab.com/snippets/16220
library("shiny")
library("ggplot2")
ui <- pageWithSidebar(
headerPanel("Tooltips in ggplot2 + shiny"),
sidebarPanel(
HTML("Tooltips are managed by combination of shiny+ggplot hover functionality",
"and css styles. By setting hover argument of 'plotOutput' we could access",
"hover data from the server side, as an ordinary input. Hover input is",
"a list with: position of cursor ON the image; domain - that is",
"values of variables at the plotting area edges; range - that is position",
"of plotting area edges in pixels relative to whole image element.",
"Additionally for ggplot used mappings are returned. </br>",
"To create tooltip first we need to identify position of the cursor",
"inside the image element. We do it by calculating distances from left and",
"top edge of image element from hover data. Then we create tooltip, in this",
"app it is 'wellPanel' with some info inside, and set 'position' property",
"to 'absolute' and set 'left' and 'top' properties to calculated values.",
"However, 'absolute' position is defined as relative to the nearest positioned",
"ancestor. Because we want to position tooltip inside the image, we need",
"to put both 'plotOutput' with image and 'uiOutput' with tooltip content",
"inside additional 'div' element with 'position' property set to 'relative'.",
"We don't set top, left etc. for this element, so the actual position of",
"the image doesn't change - it's edges are identical as previously, so",
"we can use 'div' (for positioning tooltip) as substitute for image. </br>"),
width = 3
),
mainPanel(
# this is an extra div used ONLY to create positioned ancestor for tooltip
# we don't change its position
div(
style = "position:relative",
plotOutput("scatterplot",
hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
uiOutput("hover_info")
),
width = 7
)
)
server <- function(input, output) {
output$scatterplot <- renderPlot({
ggplot(df_2, aes(x = Date, y = make)) +
geom_point()
})
output$hover_info <- renderUI({
hover <- input$plot_hover
point <- nearPoints(df_2, hover, threshold = 5, maxpoints = 1, addDist = TRUE)
if (nrow(point) == 0) return(NULL)
# calculate point position INSIDE the image as percent of total dimensions
# from left (horizontal) and from top (vertical)
left_pct <- (hover$x - hover$domain$left)/(hover$domain$right - hover$domain$left)
top_pct <- (hover$domain$top - hover$y)/(hover$domain$top - hover$domain$bottom)
# calculate distance from left and bottom side of the picture in pixels
left_px <- hover$range$left + left_pct * (hover$range$right - hover$range$left)
top_px <- hover$range$top + top_pct * (hover$range$bottom - hover$range$top)
# create style property fot tooltip
# background color is set so tooltip is a bit transparent
# z-index is set so we are sure are tooltip will be on top
style <- paste0("position:absolute; z-index:100; background-color: rgba(245, 245, 245, 0.85); ",
"left:", left_px + 2, "px; top:", top_px + 2, "px;")
# actual tooltip created as wellPanel
wellPanel(
style = style,
p(HTML(paste0(DF_1 DATE THAT MATCHES DF_2 DATE)))
)
})
}
runApp(list(ui = ui, server = server))
更新組織df_1。我爲ggplot添加了標籤,因爲它正是我用於 – Naji
的版本您可以在df_1上使用tidyr的收集,然後使用dplyr的left_join將它連接到df_2:df_1_long <- df_1 %>%gather(Date,Performance,1:4),combined < - left_join (df_2,df_1_long)。不是100%你想如何格式化繪圖,但它可能是一個開始? – user2738526
這是一個好的開始。根據下面的答案,這看起來就像穆迪正在嘗試做的那樣。但是,由於每個日期有多個條目(即,將有4行,每個都有第一個日期,並且每個都包含從原始數據框中列出的一個值),所以gather會創建兩個日期重複的列。我不能將它加入到我的df_2中,因爲這些列每個日期有一行,所以數據不能正確匹配...... – Naji