如何在ggplot2中获取和修改大小图例

时间:2023-02-09 21:34:45

I am having some trouble displaying the size legend in my plot and changing the name of my size legend.

我在绘图中显示尺寸图例并更改尺寸图例的名称时遇到了一些问题。

My data is corp already has a size column which is either of the values 5, 10, 20

我的数据是corp已经有一个大小列,其值为5,10,20

I am using ggplot2 I already have a legend for the color

我正在使用ggplot2我已经有了颜色的图例

  1. I want to add one for the size and manually change the size labels..

    我想为大小添加一个并手动更改大小标签..

  2. How do I increase the the font of the legend ? It's super tiny (FIN, IND UTIL) also the 15 for the size shouldnt be there i want to just omit it and display both legends side by side.

    如何增加图例的字体?它是超级小(FIN,IND UTIL)也是15的大小不应该在那里我想省略它并且并排显示两个传说。

如何在ggplot2中获取和修改大小图例

p <- ggplot(corp, aes(x=annRisk, y=annRet,  color = corp$subsector1, face = "bold"))

p<- p + geom_point(aes(size = corp$Colsize), alpha = 0.55)

p<-p + scale_size(range = c(8, 20))

p<-p + scale_colour_manual("", values = c("UTIL" = "#fdcc8b", "IND" = "#fc8d59", "FIN" = "#d7301f",
"ABS" = "#74a9cf", "CMBS" = "#0570b0", "LA" = "#8c96c6", "SOV"= "#88419d", "SUPRA" = "#b3cde3"))

p<-p+labs(title = "SOME TITLE")

print(p)

p<-p+theme(plot.title = element_text(face = "bold", size = 20))

p<-p+theme(axis.title.x = element_text(size = 20), axis.text.x = element_text(size = 13))
p<-p+theme(axis.title.y = element_text(size = 20), axis.text.y = element_text(size = 13))

p<-p+geom_text(aes(label=ifelse(Colsize>=10,subsector2,"")), size=5,color = "black", face = "bold", hjust=-0.1, vjust = 0.1)


p<-p+scale_x_continuous(labels = percent, name = "Annualized Risk", limits = c(0.05, 0.09))

p<-p+scale_y_continuous(labels = percent, name = "Annualized Return", limits = c(0.04, 0.08))

p<-p+ theme(legend.position = "bottom")
print(p)

1 个解决方案

#1


1  

Although I can't use your data yet, you can try adding the following code:

虽然我还无法使用您的数据,但您可以尝试添加以下代码:

p <- p + theme(legend.position = "bottom",
               legend.title = element_blank(),
               legend.text = element_text(size=14),
               legend.box = "horizontal")

p <- p + scale_size_manual(values=c(5,10,20), labels = c("5","10","20"))

#1


1  

Although I can't use your data yet, you can try adding the following code:

虽然我还无法使用您的数据,但您可以尝试添加以下代码:

p <- p + theme(legend.position = "bottom",
               legend.title = element_blank(),
               legend.text = element_text(size=14),
               legend.box = "horizontal")

p <- p + scale_size_manual(values=c(5,10,20), labels = c("5","10","20"))