deepsense.aideepsense.ai logo
  • Careers
    • Job Offers
    • Summer Internship
  • Clients’ stories
  • Services
    • Customized AI software
    • Team augmentation
    • AI advisory
    • Train your team
  • Industries
    • Retail
    • Manufacturing
    • Financial & Insurance
    • IT Operations
    • TMT & Other
    • Medical & Beauty
  • Knowledge base
    • Blog
    • R&D Hub
  • About us
    • Our story
    • Management
    • Advisory Board
    • Press center
  • Contact
  • Menu Menu
Shiny, polls and interactive ggplot2

Shiny, polls and interactive ggplot2

June 18, 2015/in Data science /by Przemyslaw Biecek

Today we will use ggplot2 to recreate the diagrams presenting support in voting intention polls conducted before presidential elections. The story behind is interesting so let’s see it again. Yesterday RStudio has released a new version of shiny. Version 0.12 comes with very interesting feature: events like click, double_click, hover are observable. Application may respond to them.

Let’s start with an example. You may move your mouse cursor over the app bellow to see the local difference between both candidates. [Because the app cannot be embedded here, open it in a new window first]. Notice that the difference in estimated support shrinked from 50% to 10% in 6 months. Moreover the difference in expected support in the election day was in fact 10 percent points smaller (AD won slightly over BK). Why results from polls were so wrong?
Shiny, polls and interactive ggplot2 1
How the app was created? Below, you will find a step by step instruction.
Let us start with loading the data. The data is available in the rds file at https://github.com/pbiecek/SmarterPoland_blog/tree/master/2015/wybory2015b. Download it first and following commands load the data and display its first rows.

load("elections1.rda")
head(elections1)
source       date            candidate support daysToElections
2         CBOS 2015-02-16 Bronislaw Komorowski    63.0             -83
3         CBOS 2015-03-02 Bronislaw Komorowski    49.0             -69
4         CBOS 2015-03-11 Bronislaw Komorowski    52.0             -60
5         CBOS 2015-03-20 Bronislaw Komorowski    48.0             -51
6         CBOS 2015-04-22 Bronislaw Komorowski    43.0             -18
9 Dobra Opinia 2015-04-10 Bronislaw Komorowski    48.3             -30

Let us create a scatterplot. We will use dots to mark support, colors to mark the candidates and shapes of the dots to mark the opinion poll centres that conducted the polls. By the way, it is actually better to mark the centers with letters than shapes it’s easier to recognize them (we use the scale_shape_manual function for that purpose).

ggplot(elections1, aes(x=daysToElections, y=support)) +
geom_point(aes(shape=source, color=candidate), size=4) +
scale_shape_manual(values=LETTERS) + theme_bw() +
xlab("days to elections") + ylab("support (%)") +
scale_y_continuous(limits=c(10,75), breaks=seq(10,70,10)) +
geom_smooth(size=2,aes(color=candidate),se=FALSE, method="lm") +
scale_color_manual(values=c("blue3", "orange3"))

Shiny, polls and interactive ggplot2 2
The last step is to create an interactive application. Shiny package allows us to do this easily. Below you fill find both ui.R and sever.R. Notice the ‘plot_hower’ part where hover_event is dispatched.
ui.R

library(shiny)
load("elections1.rda")
shinyUI(fluidPage(
titlePanel(h2("Polls prior to the presidential election 2015")),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("pola", "Show results for",
unique(elections1$source)[1:9], unique(elections1$source)[1:9]),
p("Dev:", HTML("

Fundacja SmarterPoland.pl

"))
),
mainPanel( plotOutput("plot", width = 700, height = 550,
hover = hoverOpts(id = "plot_hower")))
)
))

server.r

library(ggplot2)
library(scales)
library(lubridate)
load("elections1.rda")

shinyServer(function(input, output) {
position = reactiveValues(x = NULL)
# update position on hover
observeEvent(input$plot_hower, {
hower = input$plot_hower
if (!is.null(hower)) {
position$x = hower$x
}
})
# render plot if change in hover or pola
output$plot = renderPlot({
elections = elections1[elections1$source %in% input$pola,]
pl = ggplot(elections, aes(x=daysToElections, y=support)) +
geom_point(aes(shape=source, color=candidate), size=4) +
scale_shape_manual(values=LETTERS) + theme_bw() +
xlab(“days to elections”) + ylab(“support (%)”) +
scale_y_continuous(limits=c(10,75), breaks=seq(10,70,10)) +
geom_smooth(size=2,aes(color=candidate),se=FALSE, method=”lm”)
# here you need to calculate the average support at the given time
if (!is.null(position$x)) {
pl = pl + geom_linerange(x=position$x, ymin=10, ymax=65)
cand = c(“Bronislaw Komorowski”, “Andrzej Duda” )
npoints = predict(lm(support~daysToElections*candidate, data=elections1),
newdata = data.frame(daysToElections = position$x,
candidate=cand))
ndf = data.frame(candidate=cand, support=npoints, daysToElections = position$x)
ndf2 = data.frame(support=72, daysToElections = position$x,
text=paste0(“Days to elections: “,round(position$x),”n”,
paste0(cand, ” (“, round(npoints,1), “%)”, collapse=”n”),
“ndiff: “, round(diff(npoints),1), “%”))
pl = pl + geom_point(data=ndf, aes(color=candidate), size=4) +
geom_text(data=ndf2, aes(x=daysToElections, y=support, label=text), size=4, hjust=1+position$x/170)
}
pl + scale_color_manual(values=c(“blue3”, “orange3”))
})
})

Przemyslaw Biecek

Share this entry
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on LinkedIn
  • Share on Reddit
  • Share by Mail
https://deepsense.ai/wp-content/uploads/2019/02/shiny-polls-and-interactive-ggplot2.jpg 337 1140 Przemyslaw Biecek https://deepsense.ai/wp-content/uploads/2019/04/DS_logo_color.svg Przemyslaw Biecek2015-06-18 06:20:162021-01-05 16:53:42Shiny, polls and interactive ggplot2

Start your search here

NEWSLETTER SUBSCRIPTION

    You can modify your privacy settings and unsubscribe from our lists at any time (see our privacy policy).

    This site is protected by reCAPTCHA and the Google privacy policy and terms of service apply.

    THE NEWEST AI MONTHLY DIGEST

    • AI Monthly Digest 20 - TL;DRAI Monthly Digest 20 – TL;DRMay 12, 2020

    CATEGORIES

    • Elasticsearch
    • Computer vision
    • Artificial Intelligence
    • AIOps
    • Big data & Spark
    • Data science
    • Deep learning
    • Machine learning
    • Neptune
    • Reinforcement learning
    • Seahorse
    • Job offer
    • Popular posts
    • AI Monthly Digest
    • Press release

    POPULAR POSTS

    • AI trends for 2021AI trends for 2021January 7, 2021
    • A comprehensive guide to demand forecastingA comprehensive guide to demand forecastingMay 28, 2019
    • What is reinforcement learning? The complete guideWhat is reinforcement learning? The complete guideJuly 5, 2018

    Would you like
    to learn more?

    Contact us!
    • deepsense.ai logo white
    • Services
    • Customized AI software
    • Team augmentation
    • AI advisory
    • Knowledge base
    • Blog
    • R&D Hub
    • deepsense.ai
    • Careers
    • Summer Internship
    • Our story
    • Management
    • Scientific Advisory Board
    • Press center
    • Support
    • Terms of service
    • Privacy policy
    • Contact us
    • Join our community
    • facebook logo linkedin logo twitter logo
    • © deepsense.ai 2014-
    Scroll to top

    This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies.

    OKLearn more

    Cookie and Privacy Settings



    How we use cookies

    We may request cookies to be set on your device. We use cookies to let us know when you visit our websites, how you interact with us, to enrich your user experience, and to customize your relationship with our website.

    Click on the different category headings to find out more. You can also change some of your preferences. Note that blocking some types of cookies may impact your experience on our websites and the services we are able to offer.

    Essential Website Cookies

    These cookies are strictly necessary to provide you with services available through our website and to use some of its features.

    Because these cookies are strictly necessary to deliver the website, refuseing them will have impact how our site functions. You always can block or delete cookies by changing your browser settings and force blocking all cookies on this website. But this will always prompt you to accept/refuse cookies when revisiting our site.

    We fully respect if you want to refuse cookies but to avoid asking you again and again kindly allow us to store a cookie for that. You are free to opt out any time or opt in for other cookies to get a better experience. If you refuse cookies we will remove all set cookies in our domain.

    We provide you with a list of stored cookies on your computer in our domain so you can check what we stored. Due to security reasons we are not able to show or modify cookies from other domains. You can check these in your browser security settings.

    Other external services

    We also use different external services like Google Webfonts, Google Maps, and external Video providers. Since these providers may collect personal data like your IP address we allow you to block them here. Please be aware that this might heavily reduce the functionality and appearance of our site. Changes will take effect once you reload the page.

    Google Webfont Settings:

    Google Map Settings:

    Google reCaptcha Settings:

    Vimeo and Youtube video embeds:

    Privacy Policy

    You can read about our cookies and privacy settings in detail on our Privacy Policy Page.

    Accept settingsHide notification only