我有以下数据框,我想在R中的ggplot中说明:
df <- structure(list(Tenors = structure(c(3L, 2L, 4L, 5L, 1L, 3L, 2L,
4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L,
5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L,
1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L,
3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L,
2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L, 4L, 5L, 1L, 3L, 2L,
4L, 5L, 1L), .Label = c("12M", "1M", "1W", "3M", "6M"), class = "factor"),
Average = c(93.9426, 97.6231, 100.0376, 108.7951, 127.6704,
93.9426, 97.6231, 100.0376, 108.7951, 127.6704, 93.9426,
97.6231, 100.0376, 108.7951, 127.6704, 93.9426, 97.6231,
100.0376, 108.7951, 127.6704, 93.9426, 97.6231, 100.0376,
108.7951, 127.6704, 93.9426, 97.6231, 100.0376, 108.7951,
127.6704, 93.9426, 97.6231, 100.0376, 108.7951, 127.6704,
93.9426, 97.6231, 100.0376, 108.7951, 127.6704, 93.9426,
97.6231, 100.0376, 108.7951, 127.6704, 93.9426, 97.6231,
100.0376, 108.7951, 127.6704, 93.9426, 97.6231, 100.0376,
108.7951, 127.6704, 93.9426, 97.6231, 100.0376, 108.7951,
127.6704, 93.9426, 97.6231, 100.0376, 108.7951, 127.6704,
93.9426, 97.6231, 100.0376, 108.7951, 127.6704, 93.9426,
97.6231, 100.0376, 108.7951, 127.6704, 93.9426, 97.6231,
100.0376, 108.7951, 127.6704, 93.9426, 97.6231, 100.0376,
108.7951, 127.6704, 93.9426, 97.6231, 100.0376, 108.7951,
127.6704), Pairs = structure(c(1L, 1L, 1L, 1L, 1L, 8L, 8L,
8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 11L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 15L, 15L, 15L, 15L, 15L, 10L, 10L,
10L, 10L, 10L, 13L, 13L, 13L, 13L, 13L, 16L, 16L, 16L, 16L,
16L, 17L, 17L, 17L, 17L, 17L, 14L, 14L, 14L, 14L, 14L, 18L,
18L, 18L, 18L, 18L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L), .Label = c("audusd", "euraud", "eurchf",
"eurgbp", "eurjpy", "eurnok", "eurtry", "eurusd", "gbpusd",
"nzdusd", "usdcad", "usdchf", "usddkk", "usdhkd", "usdjpy",
"usdnok", "usdsek", "usdzar"), class = "factor")), .Names = c("Tenors",
"Average", "Pairs"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90"
))
...如果我运行str(df)我会得到我的Tenors和Pairs是因子而Average是数字。
当我运行以下内容时:
ggplot(seperate, aes(x=seperate$Tenors,y = seperate$Average, colour = Pairs,group=2)) +
geom_line()+
geom_point()+
xlab("Tenor")+
ylab("Average")+
ylim(c(50,150))
..我只收到我的df的前5行,如
......我做错了什么?
最好,ph
答案 0 :(得分:1)
seperate$Tenors
中使用类似aes()
的内容,它会混淆ggplot2,因为它获取了一个向量而不是对数据框的引用,并且不能对数据做太多的事情。
请注意,有一对奇怪的“eurusd” - 这会映射到“空白”国家/地区。
因此,使用您使用的数据:
set.seed(1234)
# Arrange the "Tenors" factor correctly
df$Tenors <- factor(df$Tenors,levels=c("1W","1M","3M","6M","12M"))
# Get a numerical valued tenor for geom_line (it seemingly doesn't like factors)
df$NTenors <- as.numeric(df$Tenors)
# jitter the values so they don't all overwrite each other
df$Average <- jitter(df$Average,2)
df$cur <- ifelse(grepl("usd",df$Pairs),"usd","eur")
# extract country from pairs
df$country <- gsub("usd","",df$Pairs) # gsub is not vectorized dammit
df$country <- gsub("eur","",df$country)
seperate <- df
ggplot(seperate, aes(x=Tenors,y=Average, color=country)) +
geom_point()+
geom_line(aes(x=NTenors))+
facet_grid(.~cur)+
xlab("Tenor")+
ylab("Average")+
ylim(c(90,130))
产生这个: