我正在尝试使用GGPLOT2格式化图表。我想要完成的事情感觉它应该是相当简单的,但尽可能地尝试,没有骰子。我有以下图表。
#packages
library(ggplot2)
library(plyr)
library(reshape2)
library(PerformanceAnalytics)
library(timeSeries)
library(quantmod)
library(ggthemes)
#stock data
getSymbols(c("^FCHI","^GSPC"), from = "2008-12-31")
stockmarketdata <- cbind(GSPC$GSPC.Close, FCHI$FCHI.Close)
#normalize data
stockmarketdata$CAC40 <- stockmarketdata[,2] / 3217.97
stockmarketdata$SNP <- stockmarketdata[ ,1] / 903.25
#Isolate normalized data
marketdata <- stockmarketdata[,3:4]
GSPC.DF<-data.frame(Date=index(GSPC),coredata(GSPC))
FCHI.DF<-data.frame(Date=index(FCHI),coredata(FCHI))
#format before making ggplot chart
market.df <- data.frame(Date=index(marketdata), marketdata)
market.df.eco <- market.df
colnames(market.df.eco) <- c("Date", "CAC40", "S&P500")
market.df.eco.mlt <- melt(market.df.eco, id = "Date")
ggplot chart
chart.EQ <-
ggplot(market.df.eco.mlt, aes(x=Date, y=value, colour = variable, group = variable)) +
geom_line() +
labs(title="Equity Market", x= "", y = "", color="December 31st 2008=1", title.vjust=1) +
theme_economist() +
scale_color_economist() +
theme(legend.position = c(0,1),
legend.justification=c(0,1),
legend.direction="horizontal",
plot.title = element_text(vjust=1),
legend.title=element_text(vjust=1),
legend.title.align=0)
看起来像这样
理想情况下,我希望标题和图例与左侧齐平。我希望图例和标题都在图表上方,图表标题正下方的图例标题和图例标题下方图例中的标签。更像这样。
如果有人可以帮助我使用这种非常好的格式。每当我从主题()中删除对齐参数时,图例就会离开图像一半。感谢您提供的任何帮助!
如果您没有此处的财务软件包,那么数据框'marketdata'的一小部分示例如果您应用上述代码中的转换,您应该能够获得类似的图表
marketdata<-
structure(c(0.999999990988107, 1.04093261932212, 1.04411163621786,
1.05539205492904, 1.03981394730218, 1.03305191720246, 1.02533584837646,
1.00874778726961, 0.993760008017477, 0.948424006438842, 0.930984404142985,
0.937469895617423, 0.929060849231037, 0.909045152378674, 0.902920185085629,
0.891748561049357, 0.885384230741741, 0.918395795175219, 0.918134733698574,
0.955885235101632, 0.935294611198986, 0.92416023828687, 0.910527459547479,
0.926792323421287, 0.953703729369758, 0.952864706321066, 0.970422359127027,
0.9741763027623, 0.938712915285102, 0.94087886804414, 0.921183257768096,
0.931599768487587, 0.920524420985901, 0.893491853559853, 0.893131405202659,
0.892674604797434, 0.854746951960397, 0.847699051575994, 0.841539867991311,
0.838081126300121, 1, 1.03160806864102, 1.02679215278162, 1.03481872349848,
1.00376421145862, 1.0071740714088, 0.985718213119291, 0.963476346526432,
0.965170194298367, 0.932875721007473, 0.934115682258511, 0.94117907002491,
NA, 0.891469660669803, 0.930240786050374, 0.916136174923886,
0.92106284195959, 0.926177699418766, 0.936296730694714, 0.967716608912261,
0.935665668419596, 0.914342657071686, 0.913855523941323, 0.928325502352615,
0.921372798228619, 0.936451675615832, 0.961638500968724, 0.963066720177138,
0.915759726543039, 0.923044550235262, 0.924649877663991, 0.915405510102408,
NA, 0.873700507057847, 0.872870172156103, 0.862374760033213,
0.85253250816496, 0.82295047550512, 0.8559535178522, 0.846830915029062
), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1440080950.2661, class = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), index = structure(c(1230681600,
1230854400, 1231113600, 1231200000, 1231286400, 1231372800, 1231459200,
1231718400, 1231804800, 1231891200, 1231977600, 1232064000, 1232323200,
1232409600, 1232496000, 1232582400, 1232668800, 1232928000, 1233014400,
1233100800, 1233187200, 1233273600, 1233532800, 1233619200, 1233705600,
1233792000, 1233878400, 1234137600, 1234224000, 1234310400, 1234396800,
1234483200, 1234742400, 1234828800, 1234915200, 1235001600, 1235088000,
1235347200, 1235433600, 1235520000), tzone = "UTC", tclass = "Date"), .Dim = c(40L,
2L), .Dimnames = list(NULL, c("CAC40", "SNP")))