Я включаю довольно длинный пример, но, по крайней мере, он полностью самодостаточен. У меня такой вопрос: могу ли я как-то настроить вывод точек ближнего действия? Это уже очень близко к тому, что я имею в виду, но мой вопрос заключается в том, можно ли настроить вывод в ближней точке (что некрасиво). Мне не нужны все подробности о тибле, просто что-то вроде, например,
год 2017 Стоимость 2097 миллиардов евро
Пока мой поиск в Google ни к чему не привел, поэтому любые предложения приветствуются. Спасибо!
library(shiny)
library(Cairo) # For nicer ggplot2 output when deployed on Linux
library(tidyverse)
library(scales)
my_ggplot_theme2 <- function(legend_coord){
theme_bw()+
theme(legend.title = element_text(vjust=1,lineheight=1, size=14 ),
panel.grid.minor = element_blank(),
plot.title = element_text(lineheight=.8, size=24, face="bold",
vjust=1),legend.text = element_text(vjust=.4,lineheight=1,size = 14 ),
axis.title.x = element_text(size = 20, vjust=1),
axis.title.y = element_text(size = 20, angle=90, vjust=1),
axis.text.x = element_text(size=15, colour="black", vjust=1),
axis.text.y = element_text(size=15, colour="black", hjust=1),
legend.position = legend_coord,
strip.background = element_rect(colour = 'blue',
fill = 'white', size = 1, linetype=1),
strip.text.x = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
strip.text.y = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
)
}
df_ini <- structure(list(reporterlabel = c("USA", "USA", "USA", "USA",
"USA", "USA", "USA", "USA", "China", "China", "USA", "USA", "USA",
"USA", "USA", "USA", "China", "China", "USA", "USA", "USA", "USA",
"China", "China", "China", "China", "China", "China", "China",
"China", "China", "China", "China", "China", "China", "China",
"Japan", "Japan", "Japan", "Japan", "Japan", "Japan", "Japan",
"Japan", "Japan", "Japan", "Japan", "Japan", "Japan", "Japan",
"Japan", "Japan", "Japan", "Japan", "China", "China", "China",
"China", "China", "China", "China", "China", "China", "China",
"China", "China", "China", "China", "EU28", "EU28", "China",
"China", "China", "China", "USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA", "USA", "USA", "EU28", "EU28", "EU28", "EU28",
"EU28", "EU28", "USA", "USA", "EU28", "EU28", "USA", "USA", "EU28",
"EU28", "EU28", "EU28", "EU28", "EU28", "USA", "USA", "USA",
"USA", "EU28", "EU28", "EU28", "EU28", "EU28", "EU28", "EU28",
"EU28", "EU28", "EU28", "EU28", "EU28", "EU28", "EU28", "EU28",
"EU28", "EU28", "EU28", "EU28", "EU28", "China", "China", "China",
"China", "USA", "USA", "USA", "USA", "China", "China", "USA",
"USA", "China", "China", "USA", "USA", "China", "China", "USA",
"USA", "USA", "USA", "USA", "USA", "China", "China", "China",
"China", "USA", "USA", "USA", "USA", "China", "China", "Japan",
"Japan", "Japan", "Japan", "Japan", "Japan", "Japan", "Japan",
"Japan", "Japan", "China", "China", "Japan", "Japan", "Japan",
"Japan", "Japan", "Japan", "Japan", "Japan"), partnerlabel = c("World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "Japan", "Japan", "Japan",
"Japan", "Japan", "Japan", "Japan", "Japan", "Japan", "Japan",
"Japan", "Japan", "Japan", "Japan", "Switzerland", "Switzerland",
"Japan", "Japan", "Japan", "Japan", "Japan", "Japan", "Japan",
"Japan", "Japan", "Japan", "Japan", "Japan", "Japan", "Japan",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Japan", "Japan", "Switzerland", "Switzerland",
"Japan", "Japan", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Japan", "Japan",
"Japan", "Japan", "Switzerland", "Switzerland", "Japan", "Japan",
"Japan", "Japan", "Japan", "Japan", "Japan", "Japan", "Japan",
"Japan", "Japan", "Japan", "Japan", "Japan", "Japan", "Japan",
"Japan", "Japan", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "Switzerland", "Switzerland", "Switzerland", "Switzerland",
"Switzerland"), year = c(2018L, 2018L, 2017L, 2017L, 2015L, 2015L,
2016L, 2016L, 2018L, 2018L, 2012L, 2012L, 2014L, 2014L, 2013L,
2013L, 2017L, 2017L, 2011L, 2011L, 2010L, 2010L, 2014L, 2014L,
2013L, 2013L, 2015L, 2015L, 2016L, 2016L, 2012L, 2012L, 2011L,
2011L, 2010L, 2010L, 2012L, 2012L, 2018L, 2018L, 2013L, 2013L,
2011L, 2011L, 2014L, 2014L, 2017L, 2017L, 2015L, 2015L, 2016L,
2016L, 2010L, 2010L, 2018L, 2018L, 2017L, 2017L, 2011L, 2011L,
2012L, 2012L, 2010L, 2010L, 2016L, 2016L, 2015L, 2015L, 2016L,
2016L, 2014L, 2014L, 2013L, 2013L, 2017L, 2017L, 2018L, 2018L,
2016L, 2016L, 2015L, 2015L, 2012L, 2012L, 2017L, 2017L, 2018L,
2018L, 2012L, 2012L, 2013L, 2013L, 2015L, 2015L, 2014L, 2014L,
2014L, 2014L, 2013L, 2013L, 2011L, 2011L, 2010L, 2010L, 2011L,
2011L, 2010L, 2010L, 2011L, 2011L, 2018L, 2018L, 2017L, 2017L,
2010L, 2010L, 2016L, 2016L, 2012L, 2012L, 2015L, 2015L, 2013L,
2013L, 2014L, 2014L, 2013L, 2013L, 2016L, 2016L, 2018L, 2018L,
2016L, 2016L, 2018L, 2018L, 2017L, 2017L, 2014L, 2014L, 2015L,
2015L, 2017L, 2017L, 2014L, 2014L, 2013L, 2013L, 2012L, 2012L,
2011L, 2011L, 2012L, 2012L, 2011L, 2011L, 2010L, 2010L, 2015L,
2015L, 2017L, 2017L, 2016L, 2016L, 2015L, 2015L, 2018L, 2018L,
2012L, 2012L, 2010L, 2010L, 2011L, 2011L, 2013L, 2013L, 2014L,
2014L, 2010L, 2010L), trade = c("i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value", "i_value", "e_value", "i_value",
"e_value", "i_value", "e_value"), value = c(2153.033680273, 1409.176796262,
2073.705710368, 1368.155269117, 2020.426481323, 1356.080871475,
1977.755888676, 1313.306256844, 1806.965668188, 2117.979322303,
1771.689509717, 1203.146360193, 1767.169505028, 1219.784616324,
1707.981350627, 1188.487972287, 1621.780725645, 2018.318404554,
1586.173935964, 1065.002559117, 1484.588368409, 963.634985291,
1477.685507736, 1763.810407469, 1467.7361096, 1664.529933147,
1443.678047307, 2055.46740743, 1435.956424248, 1930.251015449,
1414.495661466, 1595.650735082, 1251.027163365, 1364.425779918,
1051.451510212, 1190.649622597, 689.629865699, 621.590442779,
657.302694949, 624.931817975, 626.716997393, 538.071974436, 614.222575221,
590.922220402, 611.382793901, 519.543999785, 594.12699826, 617.937009274,
584.037763765, 563.136061217, 548.257633382, 582.645761187, 523.535979271,
580.653879852, 152.818513692, 124.948863726, 146.740834246, 121.739056586,
139.662191575, 105.81182183, 138.330347187, 117.924386484, 132.989383588,
90.716169952, 131.469896106, 117.099527509, 128.630611328, 122.484952311,
122.561022748, 143.220309136, 122.458137067, 112.496590186, 122.143972732,
112.877296035, 120.86712417, 59.923778223, 120.741954319, 63.477320952,
119.434301319, 57.154447135, 118.179134496, 56.306292279, 113.972363801,
54.464342198, 111.836134513, 150.967565896, 110.1018017, 157.478369565,
106.902278992, 134.948590308, 104.34034559, 49.104801983, 103.301313019,
151.66367911, 100.868435476, 50.302896129, 97.515304235, 141.598887161,
95.480368544, 170.052458043, 94.586903966, 143.012571618, 93.200648714,
45.670513691, 92.620610256, 47.2699258, 86.349819222, 111.557347675,
70.428774448, 49.020854681, 69.930994346, 64.720814854, 68.356634924,
60.559417639, 67.049850532, 43.980181628, 65.864669524, 57.993483823,
65.049885678, 55.650487632, 59.650510137, 56.492788279, 56.595873541,
54.029698964, 56.48397519, 53.296964381, 42.259609603, 2.657949611,
36.22046978, 2.938986358, 35.091273848, 18.865400225, 33.121737046,
20.531060435, 32.763819035, 3.488139441, 32.062628223, 19.218557122,
30.573392992, 2.339371756, 28.420871049, 20.120244253, 24.915989434,
2.854051193, 23.69832177, 16.71585985, 21.513645993, 20.014475478,
20.223146701, 20.56983734, 19.609108427, 2.667828353, 17.80879672,
2.730342186, 17.681697192, 17.635480366, 14.762842272, 15.63045938,
8.244246492, 2.880406036, 6.919343595, 4.506222441, 6.910251482,
2.843246229, 6.658258726, 2.432435039, 6.573509392, 3.210703583,
6.401458799, 3.405673417, 6.366489807, 2.292004086, 5.629922682,
6.624220767, 5.486891886, 2.501314565, 5.438397721, 2.28086752,
5.12168412, 5.882474702)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -180L))
reporters <- df_ini$reporterlabel %>% unique
partners <- df_ini$partnerlabel %>% unique
year_range <- seq(min(df_ini$year), max(df_ini$year))
impexp <- df_ini$trade %>% unique
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("reporterlabel",
"Reporter:",
reporters ## , multiple=T
),
selectInput("partnerlabel",
"Partner:",
partners),
selectInput("tradetype",
"Import/Export:",
impexp) ,
selectizeInput("years",
"Years:",
year_range, multiple=T, selected=year_range)
)
,
mainPanel(
plotOutput("tradeplot" ,
hover = "plot_hover"
## click = "plot_click"
) ,
verbatimTextOutput("info")
)
)
)
server <- function(input, output) {
filtered_data <- reactive({
df_ini %>% filter(reporterlabel %in% input$reporterlabel,
partnerlabel %in% input$partnerlabel,
trade %in% input$tradetype,
year %in% input$years )
})
output$tradeplot <- renderPlot({
filtered_data() %>%
ggplot(aes(x = year, y = value)) +
geom_point(size=3) +
geom_line(size=1) +
my_ggplot_theme2("right")+
scale_y_continuous(breaks=pretty_breaks(n=5))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("b \u20ac")+
labs(title = "Trade Value")
})
output$info <- renderPrint({
# With ggplot2, no need to tell it what the x and y variables are.
# threshold: set max distance, in pixels
# maxpoints: maximum number of rows to return
# addDist: add column with distance, in pixels
nearPoints(filtered_data(), input$plot_hover, threshold = 10, maxpoints = 1,
addDist = F)
})
}
shinyApp(ui = ui, server = server)