forked from alexxstefann/data-visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script1.R
155 lines (119 loc) · 4.73 KB
/
Script1.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
df <- read.csv("Clean.csv")
head(df)
###### 1. Describe n_tokens_content
print(summary(df$n_tokens_content))
###### 2.
df_q1 <- df %>% select(n_tokens_content, shares, shares_categorical)
bar1 <- df_q1 %>%
group_by(shares_categorical) %>%
summarise(mean_n_tokens_content = mean(n_tokens_content))
# Plotting the bar chart
ggplot(bar1, aes(x = shares_categorical, y = mean_n_tokens_content)) +
geom_bar(stat = "identity", fill = "steelblue") +
labs(x = "shares_categorical", y = "Mean n_tokens_content")
###### 2.
df_q1 <- df %>% select(n_tokens_content, shares, shares_categorical)
bar1 <- df_q1 %>%
group_by(shares_categorical) %>%
summarise(mean_n_tokens_content = mean(n_tokens_content))
# Creating an interactive bar chart using plotly
plot_ly(bar1, x = ~shares_categorical, y = ~mean_n_tokens_content, type = "bar") %>%
layout(xaxis = list(title = "shares_categorical"), yaxis = list(title = "Mean n_tokens_content"))
##### 2.
df_q1 <- df %>% select(n_tokens_content, shares, shares_categorical)
bar1 <- df_q1 %>%
group_by(shares_categorical) %>%
summarise(mean_n_tokens_content = mean(n_tokens_content))
# Converting ggplot to plotly object
p <- ggplot(bar1, aes(x = shares_categorical, y = mean_n_tokens_content)) +
geom_bar(stat = "identity", fill = "steelblue") +
labs(x = "shares_categorical", y = "Mean n_tokens_content")
ggplotly(p)
##### 3.
p <- plot_ly(df, y = ~n_tokens_title, x = ~shares_categorical, color = ~shares_categorical, type = "box", log_y = FALSE)
ggplotly(p)
##### 4.
df_q2 <- df %>% select(n_tokens_title, shares, shares_categorical)
bar2 <- df_q2 %>%
group_by(shares_categorical) %>%
summarise(mean_n_tokens_title = mean(n_tokens_title))
# Plotting the bar chart
p <- plot_ly(bar2, x = ~shares_categorical, y = ~mean_n_tokens_title, type = "bar") %>%
layout(xaxis = list(title = "shares_categorical"), yaxis = list(title = "Mean n_tokens_title"))
ggplotly(p)
##### 5.
p <- plot_ly(df, y = ~n_non_stop_unique_tokens, x = ~shares, color = ~shares_categorical, type = "scatter")
p <- layout(p, yaxis = list(range = c(0, 1)))
ggplotly(p)
##### 6.
p <- plot_ly(df, y = ~n_non_stop_unique_tokens, x = ~shares_categorical, color = ~shares_categorical, type = "violin", box = list(visible = TRUE))
#p <- layout(p, yaxis = list(type = "log"))
ggplotly(p)
##### 7.
df_q4 <- df %>% select(num_hrefs, num_self_hrefs, shares_categorical, shares)
df_q4 <- df_q4 %>% mutate(num_ext_refs = num_hrefs - num_self_hrefs)
##### 8.
q4 <- df_q4 %>%
group_by(shares_categorical) %>%
summarise(num_self_hrefs = mean(num_self_hrefs), num_ext_refs = mean(num_ext_refs)) %>%
ungroup()
print(q4)
##### 9.
p <- plot_ly(q4, x = ~shares_categorical, y = ~num_ext_refs, type = "bar", name = "num_ext_refs", opacity = 1) %>%
add_trace(y = ~num_self_hrefs, name = "num_self_hrefs") %>%
layout(xaxis = list(title = "shares_categorical"), yaxis = list(title = "Count"), barmode = "relative")
ggplotly(p)
##### 10.
p <- plot_ly(df, y = ~average_token_length, x = ~shares_categorical, color = ~shares_categorical, type = "box", boxpoints = "all")
ggplotly(p)
##### 11.
# Not working
#df_6 <- df %>%
# select(Channel, shares, shares_categorical) %>%
# pivot_wider(names_from = shares_categorical, values_from = shares, values_fn = mean)
#
#Error in `values[spec$.name]`:
# ! Can't subset columns with `spec$.name`.
#✖ Subscript `spec$.name` can't contain the empty string.
#✖ It has an empty string at location 6.
#
# Working
df_6 <- df %>%
select(Channel, shares, shares_categorical) %>%
mutate(shares_categorical = ifelse(shares_categorical == "", "N/A", shares_categorical)) %>%
pivot_wider(names_from = shares_categorical, values_from = shares, values_fn = mean)
##### 12.
df_6 <- df %>%
select(Channel, shares_categorical) %>%
group_by(Channel, shares_categorical) %>%
summarise(Counts = n()) %>%
ungroup()
##### 13.
pivot_df <- df_6 %>%
mutate(shares_categorical = ifelse(shares_categorical == "", "N/A", shares_categorical)) %>%
pivot_wider(names_from = shares_categorical, values_from = Counts, values_fn = mean)
##### 14.
p <- plot_ly(df_6, x = ~Channel, y = ~Counts, color = ~shares_categorical, type = "bar", barmode = "group")
ggplotly(p)
##### 15.
df_61 <- df_6 %>%
group_by(Channel) %>%
summarise(Counts = sum(Counts)) %>%
ungroup()
newdf <- df_61 %>%
slice(rep(row_number(), each = 5)) %>%
mutate(Counts = rep(Counts, length.out = n()))
df_6 <- bind_rows(df_6, newdf)
##### 16.
df_61 <- df_6 %>%
group_by(Channel) %>%
summarise(Counts = sum(Counts)) %>%
ungroup()
newdf <- rep(unlist(df_61), each = 5)
names(newdf) <- colnames(df)
#df_6 <- data.frame(df_6, newdf)
##### 17.
#print(df_6)
##### 18.
#p <- plot_ly(df_6, x = ~Channel, y = ~pct, color = ~shares_categorical, type = "bar", barmode = "relative")
#ggplotly(p)