-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.R
88 lines (67 loc) · 3.18 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
library(shiny)
library(ggplot2)
library(plotly)
library(ggthemes)
shinyServer(function(input, output, session) {
output$trendPlot <- renderGraph({
if (length(input$name)==0) print("Please select at least one country")
else {
df_trend <- subset(Ideal_Point_Data, Name %in% input$name)
# Graph title
if (length(input$name)>2) {
j_names_comma <- paste(input$name[-length(input$name)], collapse = ', ')
j_names <- paste(j_names_comma, ", and ", input$name[length(input$name)],
sep="")
}
else{
j_names <- paste(input$name, collapse = ' and ')
}
graph_title <- paste("Ideal Points for ", j_names, sep="")
ggideal_point <- ggplot(df_trend)+
geom_hline(aes(yintercept = 2), color = "white")+
geom_hline(aes(yintercept = -2), color = "white")+
geom_line(aes(x=Year, y=Ideal.point, by=Name, color=Name))+
labs(x = "Year")+
labs(y = "Ideology")+
labs(title = graph_title)+
scale_colour_hue("clarity",l=70, c=150)+
theme_few()
# This converts the ggplot2 graph into Plotly's format.
# This is a list of lists which declaratively describe every attribute
# of the plotly graph
fig <- gg2list(ggideal_point)
data <- list()
for(i in 1:(length(fig)-1)){data[[i]]<-fig[[i]]}
layout <- fig$kwargs$layout
layout$annotations <- NULL # Remove the existing annotations (the legend label)
layout$annotations <- list()
# Add colored text annotations next to the end of each line
# More about plotly annotations: https://plot.ly/r/reference/#annotation
# Each key that we update is documented in that link above.
for(i in 1:(length(data))){ # data is a list of the lines in the graph
layout$annotations[[i]] <- list(
text = data[[i]]$name, # The text label of the annotation, e.g. "Canada"
font = list(color = data[[i]]$line$color), # Match the font color to the line color
showarrow = FALSE, # Don't show the annotation arrow
y = data[[i]]$y[[length(data[[i]]$y)]], # set the y position of the annotation to the last point of the line
yref = "y1", # the "y" coordinates above are with respect to the yaxis
x = 1, # set the x position of the graph to the right hand side of the graph
xref = "paper", # the x coordinates are with respect to the "paper", where 1 means the right hand side of the graph and 0 means the left hand side
xanchor = "left" # position the x coordinate with respect to the left of the text
);
}
layout$showlegend <- FALSE # remove the legend
layout$margin$r <- 170 # increase the size of the right margin to accommodate more room for the annotation labels
# Send this message up to the browser client, which will get fed through to
# Plotly's javascript graphing library embedded inside the graph
return(list(
list(
id="trendPlot",
task="newPlot",
data=data,
layout=layout
)
))
}
})
})