Y轴未排序,ggplot
The Y-axis is not sorted, ggplot
我用这个 info_mat 来计算进化率。
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.)
然后我进行如下处理:
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)
但是,在输出中,y 轴完全混乱了。
这些值不是从最小到最大排序的。
为什么?如何解决?
然后,我想简化 y 轴:我想将它从 -1 划分为 2,间隔为 0.25
我试过了
scale_y_continuous(breaks=seq(-1, 2, 0.25))
但是我有以下错误代码:
错误:提供给连续刻度的离散值
谢谢!!!!
None你的数据是数值型的,都是因数。因此,您针对离散因子值 y 进行绘图,而不是根据数字连续比例绘制。
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
我用这个 info_mat 来计算进化率。
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.)
然后我进行如下处理:
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)
但是,在输出中,y 轴完全混乱了。 这些值不是从最小到最大排序的。 为什么?如何解决?
然后,我想简化 y 轴:我想将它从 -1 划分为 2,间隔为 0.25 我试过了
scale_y_continuous(breaks=seq(-1, 2, 0.25))
但是我有以下错误代码: 错误:提供给连续刻度的离散值
谢谢!!!!
None你的数据是数值型的,都是因数。因此,您针对离散因子值 y 进行绘图,而不是根据数字连续比例绘制。
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