2017-10-10 132 views
1

您好,我在下面有以下數據,並希望創建一個ggplot並刪除圖例。我還希望圖例標籤出現在geom_line的末尾(但要確保可以看到全文 - 通過創建更多空間或縮小文本大小或兩者都可以)。ggplot2線圖圖例文本標籤

library(ggplot2) 
library(scales) 
library(broom) 

UKStyleCumRet<-structure(c(0, -0.000585875039538108, -0.006962293751684, -0.00487316877934685, 
-0.0109322156426903, -0.00954861380031302, -0.00863618174408887, 
-0.0126160092579903, -0.00346746110207252, -0.00698804834931765, 
0, 0.00642130648368866, 0.0104608988908397, 0., 
0.0180710928245018, 0.0211313335579064, 0.0218241871758766, 0.0273053649897337, 
0.0240991442158665, 0.021318026563254, 0, -0.0034821589928784, 
-0.00271401741869348, -0.0052708110221289, -0.00802020396258762, 
-0.00668050200086479, -0.00213736634044293, 0.00124544742777655, 
0.0047421495214317, 0.00280451017173089, 0, 0.00708163636457226, 
0.0092146297198139, 0.0146175909198509, 0.0134244056872652, 0.00981198385176496, 
0.00687693458035366, 0.00814732987224276, 0.0113145349249557, 
0.0115596102361639, 0, -0.00147125756270416, -0.00193209555472618, 
-0.000413483341123344, 0.00397062028565909, 0.0162993066081654, 
0.0244747264738796, 0.0384739793054933, 0.0368483148925325, 0.0395193224980617, 
0, -0.00509324778817288, -0.0129206103387607, -0.0153404412987002, 
-0.0115197832717627, -0.020793371655729, -0.0168090662065323, 
-0.0152573615095838, -0.0226830591525301, -0.0251846595390262, 
0, 0.00822555974347772, 0.00614539079362508, 0.000254730582853524, 
0.0172546701472243, 0.0109266512888126, 0.0194770984091213, 0.0259083822721515, 
0.0211296899871345, 0.0246010209576302, 0, -0.00346737847383671, 
-0.00873995016754447, -0.0139191247002052, -0.0136553668938997, 
-0.0122788514168595, -0.012192010337693, -0.0127271782491021, 
-0.0220646485936103, -0.0218854932200848, 0, 0.00177167104904052, 
-0.00680567977636182, -0.0125590474529261, -0.0150206907148072, 
-0.0256276241816732, -0.0233943013777757, -0.0253102426751336, 
-0.0322222349862453, -0.0341522649085629), index = structure(c(1485820800, 
1488240000, 1490918400, 1493337600, 1496188800, 1498780800, 1501459200, 
1504137600, 1506643200, 1507248000), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", class = c("xts", 
"zoo"), .Dim = c(10L, 9L), .Dimnames = list(NULL, c("Exchange Rate Sensitivity", 
"Growth", "Leverage", "Liquidity", "Medium-Term Momentum", "Short-Term Momentum", 
"Size", "Value", "Volatility"))) 

目前代碼:

UKCum<-tidy(UKStyleCumRet) 
UKCum$index<-as.POSIXct(UKCum$index, tz = "UTC") 
ggplot(data=UKCum, aes(x=index, y=value, color=series))+ geom_line(lwd=2) + 
scale_x_datetime(labels = date_format("%Y-%b"), breaks = date_breaks("1 month")) + geom_dl(aes(label = series), method = list(dl.combine("last.points"))) 
+0

我不能運行代碼。我不知道「tidy」來自哪裏(可能是「掃帚」?),「as.POSIXct」想要一個「起源」。您還需要'''庫'調用'ggplot2' _and_'scales'。 – Axeman

+0

道歉 - 看到更新的代碼,讓我知道這是否工作 – user8491385

回答

0

刪除圖例是通過添加theme(legend.position = "none")到您的劇情代碼。您可以通過在scale_x_datetime()行中指定軸限制來擴展x軸,以確保您的標籤不會被切斷。

此外,您還需要添加一個原點,當您使用as.POSIXct格式化數字時代

UKCum$index <- as.POSIXct(UKCum$index, tz = "UTC", 
        origin = "1970-01-01 00:00:00") #add origin to times 
ggplot(data = UKCum, aes(x = index, y = value, color = series)) + 
    geom_line(lwd = 2) + 
    scale_x_datetime(date_breaks = "1 month", date_labels = "%Y-%b", 
    #set high upper x limit to provide room for labels 
    #may need to adjust depending on size you export graph as 
        limits = c(min(UKCum$index), as.POSIXct("2018-05-01"))) + 
    geom_dl(aes(label = series), method = list(dl.combine("last.points")))+ 
    theme(legend.position = "none") #remove legend 

得到的情節:enter image description here

0

無法複製在這裏你的代碼。無論如何,請嘗試directlabels包。

install.packages('directlabels') 

p <- <draw your plot here> 

directlabels::directlabel(p, 'last.qp') 

您可以通過列出的第二個參數就是這樣的調整大小和其他屬性:

directlabels::directlabel(p, list('last.qp', cex = .5, vjust = 0, hjust = 1) 

更多屬性可用。