Rに複数の線を描画するときに問題があります。 プロットに縦線を追加しようとしていますが、機能しません。geom_vlineがggplotグラフに縦線を追加していない
私は次のコードを使用しています。
library(ggplot2)
library("ggthemes")
library("scales")
dat <- data.frame(
time = factor(c("2007","2008","2009","2010","2011","2012","2013","2014","2015","2016"),
levels=c("2007","2008","2009","2010","2011","2012","2013","2014","2015","2016")),
#frequency = c(3, 8, 20, 26, 30, 38,29, 27, 13, 1)
frequency = c(3, 8, 22, 29, 29, 37, 29, 33, 19, 30),
frequency_performance = c(3, 7, 20, 22, 19, 29, 18, 15, 16, 18),
frequency_correctness = c(0, 1, 1, 2, 1, 6, 5, 9, 1, 4),
frequency_fault = c(0, 0, 2, 4, 6, 2, 6, 7, 2, 8)
)
p <- ggplot(data=dat, aes(x=time)) + #, y=frequency, group=1)) +
theme(aspect.ratio=.4,text = element_text(size=13),
panel.grid.major = element_line(colour = "grey"),
panel.border = element_rect(fill = NA),
panel.background = element_rect(fill = "white", colour = "grey")) +
geom_line(aes(y=frequency, colour="slateblue"),group=1, size=1.5) +
geom_line(aes(y=frequency_performance, colour="darkgoldenrod"),group=1, size=1.5) +
geom_line(aes(y=frequency_correctness, colour="darkgreen"),group=1, size=1.5) +
geom_line(aes(y=frequency_fault, colour="chocolate"),group=1, size=1.5) +
xlab("Year") + ylab("Number of publications") + # Set axis labels
scale_color_discrete(name = "Category", labels = c("Fault tolerance", "Performance", "Correctness", "All papers")) +
ggtitle("")
p + geom_vline(xintercept=c(2010,2012,2014,2016), linetype="dotted", color="red")
私はこのコードで別のフォーマットを使用しようとしましたが、動作しませんでした。
dat <- data.frame(
time = factor(c("2007","2008","2009","2010","2011","2012","2013","2014","2015","2016"),
levels=c("2007","2008","2009","2010","2011","2012","2013","2014","2015","2016")),
frequency = c(3, 8, 22, 29, 29, 37, 29, 33, 19, 30, 3, 7, 20, 22, 19, 29, 18, 15, 16, 18, 0, 1, 1, 2, 1, 6, 5, 9, 1, 4, 0, 0, 2, 4, 6, 2, 6, 7, 2, 8),
group = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4)
)
p<- ggplot(data=dat, aes(x=time, y=frequency,group=group, color=as.factor(group))) +
geom_line() +
geom_point() +
scale_color_manual(breaks = c("1", "2", "3", "4"),
values=c("slateblue", "darkgoldenrod", "darkgreen", "chocolate")) +
xlab("Year") + ylab("Number of publications") + theme_bw() +
guides(shape = FALSE, linetype = FALSE,
colour = guide_legend(override.aes = list(shape = c(16, 16, 16, 16),
linetype = c("solid","solid","solid", "solid"),
labels= c("All articles","Performance","Correctness","Fault tolerance"),
title = "Category"
)))
p + geom_vline(xintercept=as.numeric(2012), linetype="dotted", color="red", size=3)
は、最初のコードで伝説を注文することができないように私は両方のコードを持っている他の問題は、(私は2つの異なるコードを持っている理由です)、があり、または色が何らかの理由で間違っています。 2番目のグラフ私は何らかの理由で凡例のテキストを置き換えることはできませんが、今は垂直線が主な問題です。
それが数値ではなかったので、だから、働いていませんでしたか? – Dante003
@ Dante003そうだね。 – www