嗨,我正在尝试使用ggplot2执行以下操作
1)将图例名称更改为“ Assets ”
2)在顶部添加标题
3)将每个面板的边框更改为更深的黑线
4)要更改每个面板图标题“corp”等的名称和颜色
这就是我所拥有的,我不确定该怎么做。非常感谢您的帮助
p <- ggplot(mystratcodes, aes(x=annRisk, y = annRet))
p<- p + facet_grid(. ~ sector) + facet_wrap(~sector)
p<- p + geom_point(size=6, aes(color = (mystratcodes$subsector1)))
p<-p+scale_x_continuous(labels = percent, name = "Annualized Risk")
p<-p+scale_y_continuous(labels = percent, name = "Annualized Return")
p<-p+ theme( legend.position = "bottom", legend.key = element_rect(colour = "grey"))
p<-p + scale_colour_manual(values = c("UTIL" = "#fdcc8a", "IND" = "#fc8d59", "FIN" = "#d7301f","ABS" = "#74a9cf", "CMBS" = "#0570b0", "LA" = "#8c96c6", "SOV"= "#88419d", "SUPRA" = "#b3cde3"))
print(p)
非常感谢
最佳答案
1)您可以通过在scale_color_manual
函数中将“Asset”作为第一个参数来将图例名称更改为“Asset”。
scale_colour_manual("Asset",values = ...)
2)你可以做
p<-p+labs(title="PUT TITLE HERE")
为标题
3)您可以在
theme()
中添加一个额外的参数来更改背景颜色(这将使边框出现在框的周围)p<-p+theme(panel.background = element_rect(fill=NA, col="black"))
资料来源:How to place divisions between facet grid lines
您也可以尝试在表达式的前面添加
p=p+theme_bw()
,但这可能会更改太多内容。4)对于网格标签,您有2个选项(虽然更多,但这是最简单的)。首先,您可以重命名数据级别。如果您不想这样做,则可以使labeller函数作为
facet_grid()
中的参数传递。请参阅此处的示例:https://stackoverflow.com/a/12104207/1362215
对于面板的颜色,您还可以使用
theme()
p<-p+theme(strip.background = element_rect(fill = 'purple'))#turns boxes purple
关于r - 在ggplot2中添加图例标题和主标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566096/