编辑:添加数据。
作为以前的Stata用户和编码新手,我很开心。向R过渡的麻烦我试图根据截图中看到的数据框中的一些变量制作线图。我已经按照我想要的方式获得了这些线条,但是添加一个简单的图例,我可以标记每个线型并不是很顺利。我甚至无法得到一个传奇出现!我非常肯定我没有在这里获得ggplot所使用的抽象级别,但我也完全确定我想手动指定这些东西,以便在多个图表之间获得格式
一定要告诉我,如果我的整个方法都是错误的,但是如果有一两行我可以添加清除它而不是添加更多抽象级别我会成为一个非常开心的露营者。
(我已经在Stack Overflow和其他地方的类似问题上尝试了很多解决方案,但我还没有得到它。)
codingsuccess <- ggplot(data = ystats, aes(x=iyear)) +
geom_line(aes(y = pnattacks), linetype = "dotted", size = 1) +
geom_line(aes(y = ptra), linetype = "longdash", size = 1) +
geom_line(aes(y = pdom), linetype = "F1", size = 1) +
geom_line(aes(y = punc), linetype = "solid", size = 1) +
labs(title = "Coding Success", x = "Year", y = "Percentage") +
theme_bw()
codingsuccess # View plot
数据
structure(list(pnattacks = c(96.6954022988506, 94.229722373435,
95.4063604240283, 93.9429464634623, 94.5975744211687, 96.4044943820225,
96.3838166845686, 93.6634494334872, 92.4137931034483, 95.6087824351297,
89.628349178911, 93.6086529006883, 93.4337997847148, 95.7178841309824,
93.8461538461539, 96.2779156327543, 95.0248756218905, 96.039603960396,
96.7592592592593, 96.1538461538462, 96.219035202086, 95.4599761051374,
86.3636363636364, 94.058229352347, 95.1696377228292, 94.8897256589564,
93.6298076923077, 91.9762258543834, 87.6906318082789, 89.1412056151941
), ptra = c(91.6216216216216, 94.2408376963351, 94.6564885496183,
90.9090909090909, 87.7952755905512, 94.5887445887446, 97.5450081833061,
96.7051070840198, NaN, 97.6311336717428, 93.2668329177057, 95.9090909090909,
93.3920704845815, 99.4413407821229, 97.0588235294118, 98.7421383647799,
95.4022988505747, 98.4455958549223, 95.852534562212, 95.4022988505747,
94.9074074074074, 94.6341463414634, 93.1578947368421, 94.5205479452055,
94.0639269406393, 88.5826771653543, 91.554054054054, 89.3041237113402,
85.7374392220421, 91.2866449511401), pdom = c(98.116539140671,
97.3818181818182, 98.1110475100172, 97.2609561752988, 97.7892756349953,
98.0813953488372, 97.5047080979284, 95.0148367952522, NaN, 96.2833914053426,
94.4610778443114, 94.6575342465753, 94.4532488114105, 99.5024875621891,
92.375366568915, 96.4285714285714, 98.0952380952381, 96.9072164948454,
97.2027972027972, 98.1060606060606, 97.9779411764706, 98.3660130718954,
96.9072164948454, 95.5916473317865, 98.2203969883641, 97.3514211886305,
95.5904334828102, 94.8237394020527, 88.2456915598763, 88.370142577579
), punc = c(15.8536585365854, 23.1884057971014, 41.5730337078652,
0.641025641025641, 25, 20.7920792079208, 19.8412698412698, 22.5641025641026,
0, 31.9587628865979, 21.0526315789474, 43.4782608695652, 49.5867768595041,
10.5263157894737, 7.69230769230769, 9.09090909090909, 0, 40,
8.69565217391304, 0, 0, 7.31707317073171, 0.75187969924812, 1.96078431372549,
14.2857142857143, 25.1968503937008, 7.01754385964912, 50.6398537477148,
60.5584642233857, 73.558981233244), iyear = 1985:2014), .Names = c("pnattacks",
"ptra", "pdom", "punc", "iyear"), row.names = c(NA, 30L), class = "data.frame")
答案 0 :(得分:0)
根据评论中的建议,tidyr::gather
会将您的数据从宽格式转换为长格式,这使得ggplot
变得更加容易。
我建议使用颜色而不是线型来区分这些组,但这里是使用线型:
library(tidyr)
library(ggplot2)
ystats %>%
gather(coding_success, percentage, -iyear) %>%
ggplot(aes(iyear, percentage)) +
geom_line(aes(linetype = coding_success), size = 1) +
scale_linetype_manual(values = c("F1", "dotted", "longdash", "solid")) +
labs(x = "Year", y = "percentage", title = "Coding Success")