I used this info_mat to compute evolution rates.
date1 <- rbind("February", "March", "April", "May", "June", "July", "August", "September", "October", "November")
sum1.visit_bush. <- rbind("0", "0" ,"1" , "-0.75" ,"2","0" ,"0.333333333333333" , "1.25" , "0", "-1")
sum1.counts_bush. <- rbind("0" ,"0.115290451813933", "-0.557273997206146", "0.146270002253775" , "0.100865119937082", "0.512412930880514", "0.435049598488427", "-0.0831961816984858", "0.824791311372408", "-0.156025577963601" )
sum1.hcounts_bush. <- rbind("0", "0.0387010676156584", "-0.625695931477516", "0.47254004576659", "-0.233100233100233", "0.99290780141844" , "-0.032536858159634" , "0.349973725696269" , "0.660957571039315", "-0.341223341926412")
evolution1 <- data.frame(date1, sum1.visit_bush., sum1.counts_bush., sum1.hcounts_bush.)
I then proceed as follows:
df_month_cand <- evolution1 %>% select(c("date", paste0(c("sum.visit_", "sum.counts_", "sum.hcounts_"), "bush.")))
df_month_cand_plot <- melt(df_month_cand, id.vars = "date", variable.name = "Type", value.name = "y")
FunctionPlot <- function(cand, evolution) {
df_month_cand <- evolution %>% select(c("date1", paste0(c("sum1.visit_", "sum1.counts_", "sum1.hcounts_"), cand)))
df_month_cand_plot <- melt(df_month_cand, id.vars = "date1", variable.name = "Type", value.name = "y")
p <- ggplot(df_month_cand_plot, aes(x = date1, y = y, color = Type)) geom_point() geom_line(aes(group=Type))
labs(
title = paste0("Evolution of visits and coverage
per month for ", cand) ,
subtitle = "We read: from March to April, whereas the visits of -candidate- increased by -value*100 %-,
the coverage in newspapers decreased by -value*100 %-",
color="Type",
x="Months",
y="Percentage change over months")
theme(
plot.title = element_text(size=15, face="bold", margin = margin(5, 0, 10, 10), vjust=2, hjust=0.5),
axis.text.x=element_text(angle=50, size=11.5, vjust=0.5),
axis.title.y = element_text(vjust=4),
plot.margin = unit(c(1, 0.3, 0.5, 0.6), "cm"),
legend.position = "bottom",
legend.box.background = element_rect(color="black", size=2),
legend.title = element_text(face = "bold", size=10),
legend.background = element_rect(fill="grey90",
size=0.5, linetype="solid",
colour ="black"),
panel.background = element_rect(fill = "gray90", colour = "gray70", size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'dashed', colour = "gray75"))
scale_color_manual(labels = c("Visits", "Main text count", "Headline count"), values = c("tomato3", "deepskyblue3", "green2"))
scale_x_discrete(limits = c("February", "March", "April", "May", "June", "July", "August", "September", "October", "November"))
scale_y_discrete()
plot(p)
}
sapply("bush.", FunctionPlot, evolution1)
However, on the output the y axis is completely messed-up. The values are not sorted from least to greatest. Why? How to resolve this?
Then, I want to simplify the y axis: I'd like to divide it from -1 to 2 with breaks of 0.25 I tried
scale_y_continuous(breaks=seq(-1, 2, 0.25))
But I have the following error code: Error: Discrete value supplied to continuous scale
Thanks!!!!
CodePudding user response:
None of your data is numeric, all are factors. So you plot against a discrete factor value y and not as desired to numeric continuous scale.
str(evolution1)
# 'data.frame': 10 obs. of 4 variables:
# $ date1 : Factor w/ 10 levels "April","August",..: 3 6 1 7 5 4 2 10 9 8
# $ sum1.visit_bush. : Factor w/ 7 levels "-0.75","-1","0",..: 3 3 5 1 7 3 4 6 3 2
# $ sum1.counts_bush. : Factor w/ 10 levels "-0.0831961816984858",..: 4 6 3 7 5 9 8 1 10 2
# $ sum1.hcounts_bush.: Factor w/ 10 levels "-0.032536858159634",..: 5 6 4 8 2 10 1 7 9 3