I have a df1:
Year V1 UR CR PA FO NA TOTALLU
1 1850 7377211 0 0.03615170 0.1405466 0 0.8232772 0.9999755
2 1851 7377212 0 0.03622309 0.1397468 0 0.8240073 0.9999771
3 1852 7377213 0 0.03631065 0.1389448 0 0.8247278 0.9999833
4 1853 7377214 0 0.03639766 0.1381439 0 0.8254495 0.9999911
5 1854 7377215 0 0.03647239 0.1373441 0 0.8261718 0.9999883
6 1855 7377216 0 0.03655438 0.1365248 0 0.8269085 0.9999877
How can I plot 3 temporal lines of CR, PA and NA in one graph, and adding labels as well. I am doing this but my labels are only for two of the variables
ggplot(df1, aes(x=Year),ylim=c(0,1)) +
geom_line(aes(y = CR), color = "red")+
geom_line(aes(y = PA, color="blue"))+
geom_line(aes(y = NA, color="green"))+
scale_y_continuous(limits = c(0, 1))+ylab("LU")
You can try this approach, with directlabels
and ggplot2
. Here the code where you have to reshape to long and then add the lines with geom_line()
. As you want a label per each line you can use geom_dl()
. Next the solution (I have selected only the variables you mentioned but you can modify that):
library(ggplot2)
library(dplyr)
library(tidyr)
library(directlabels)
#Code
df %>% select(c(1,4,5,7)) %>%
pivot_longer(cols = -1) %>%
ggplot(aes(x=factor(Year),y=value,color=name,group=name))+
geom_line()+
geom_dl(aes(label = name), method = list(dl.trans(x = x + 0.2),
"last.points", cex = 0.8,fontface='bold'))
Output:
Some data used:
#Data
df <- structure(list(Year = 1850:1855, V1 = 7377211:7377216, UR = c(0L,
0L, 0L, 0L, 0L, 0L), CR = c(0.0361517, 0.03622309, 0.03631065,
0.03639766, 0.03647239, 0.03655438), PA = c(0.1405466, 0.1397468,
0.1389448, 0.1381439, 0.1373441, 0.1365248), FO = c(0L, 0L, 0L,
0L, 0L, 0L), NA. = c(0.8232772, 0.8240073, 0.8247278, 0.8254495,
0.8261718, 0.8269085), TOTALLU = c(0.9999755, 0.9999771, 0.9999833,
0.9999911, 0.9999883, 0.9999877)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.