简体   繁体   中英

How to make an interactive graph in R-studio

The data has 4 columns and roughly 600 rows. The data is twitter data collected using the twitteR package, and then summarized into a data frame. The summary is based on how many words from these libraries each tweet has, the tweets are given a score and then the summary is the number of tweets which get specific scores. So the columns are the two types of scores, the dates, and then the number of tweets with those scores.

Score1  Score2  Date        Number    
0       0       01/10/2015  50
0       1       01/10/2015  34
1       0       01/10/2015  10
...and so on 

With dates and data that extend over a month, and the scores either way can go +/- 10 or so.

I'm trying to plot that kind of data using a bubble plots, score1 on the x axis and score2 on the y axis with the size of the bubble dependant on the number (how many tweets of with those scores there were per day). My problem is that I only know how to use ggplot.

g <- ggplot(
 twitterdata, 
  aes(x=score1, y=score2, size=number, label=""), guide=FALSE) +
  geom_point(colour="black", fill="red", shape=21) + 
  scale_size_area(max_size = 30) + 
  scale_x_continuous(name="score1", limits=c(0, 10)) + 
  scale_y_continuous(name="score2", limits=c(-10, 10)) + 
  geom_text(size=4) + 
  theme_bw()

and that just gives me the plot for all dates, and what I need is a good way to see how that data changes over time. I've looked into using sliders and selectors but I really have no idea what would be the best tool to use. I've tried subsetting the data based on date, which works nicely but ideally I could make some kind of interactive graph.

I really need some way select certain days out of that data to plot so it doesn't pile up all on itself, but do it interactively so it can be presented. Any help would be greatly appreciated, thank you.

It sounds like this won't completely satisfy your use case, but an extremely low-overhead way to add some interactivity to your plot would be to install.packages('plotly') and add the following line to your code:

# your original code
g <- ggplot(
 twitterdata, 
  aes(x=score1, y=score2, size=number, label=""),
  guide=FALSE)+
  geom_point(colour="black", fill="red", shape=21) + 
  scale_size_area(max_size = 30) + 
  scale_x_continuous(name="score1", limits=c(0,10)) + 
  scale_y_continuous(name="score2", limits=c(-10,10)) + 
  geom_text(size=4) + 
  theme_bw()

# add this line
gg <- ggplotly(g)

Details and demos: https://plot.ly/ggplot2/

As Eric suggested, if you want sliders and such you should check out shiny. Here's a demo combining shiny with plotly: https://plot.ly/r/shiny-tutorial/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM