ggplot2:修改散点图 {ggplot2} 中两个因素的图例元素?
ggplot2: modify legend's elements for two factors in scatterplot {ggplot2}?
我希望我的散点图显示两个因素:点大小和灰色阴影:
a1<-c(seq(1,10,1))
a2<-c(seq(11,20,1))
a3<-c(rep(c(1,2),each = 5))
a4<-c(rep(c(5,10,15,20,25),2))
df<-data.frame(a1,a2,a3,a4)
t1<-theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(size=.4))
ggplot(df, aes(x= a1, y= a2)) +
geom_point(aes(alpha=factor(a3), size = factor(a4))) + t1 + labs(x = "x label", y = "y label") +
theme(legend.background = element_rect())
到目前为止,或多或少还不错。
我的问题是:
- 如何去除图例中的背景?
theme(legend.background = element_rect())
由于某种原因不起作用...
如何修改我的图例headers?我通过这个例子想象:http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/
那应该是这样的:
scale_shape_discrete(name ="modified A4", breaks=c("1", "2"), labels = c("one","two"))
但我不知道如何让它工作?
我确定我完全误解了散点图中两个变量的显示,但我找不到更正它的方法?
谢谢!
根据@user20650 和@inscaven 的建议以及更多google 搜索,我希望更好地理解 ggplot 的组织方式以及如何生成我的情节:
# dummy data
a1<-c(seq(1,10,1))
a2<-c(seq(11,20,1))
a3<-c(rep(c(1,2),each = 5))
a4<-c(rep(c(5,10,15,20,25),2))
# create data frame
df<-data.frame(a1,a2,a3,a4)
# set nice theme
t1<-theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(size=.4))
# create scatter plot
ggplot(df, aes(x= a1, y= a2)) + # create basic plot
geom_point(aes(size = factor(a4), colour = factor(a3))) + # colour has to be inside of aes !! (ASSIGNED = MAPPED)
scale_colour_grey(name = "Set second\nline in title") + # change title of 1st legend, change colours
scale_size_discrete(name = "Name by size") + # change title of 2nd legend, size of point has been already assigned
theme(legend.key = element_blank()) + # delete grey boxes around the legend
labs(x = "x label", y = "y label") + # set labels on x and y axes
t1 # add nice theme
这导致:
我希望我的散点图显示两个因素:点大小和灰色阴影:
a1<-c(seq(1,10,1))
a2<-c(seq(11,20,1))
a3<-c(rep(c(1,2),each = 5))
a4<-c(rep(c(5,10,15,20,25),2))
df<-data.frame(a1,a2,a3,a4)
t1<-theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(size=.4))
ggplot(df, aes(x= a1, y= a2)) +
geom_point(aes(alpha=factor(a3), size = factor(a4))) + t1 + labs(x = "x label", y = "y label") +
theme(legend.background = element_rect())
到目前为止,或多或少还不错。
我的问题是:
- 如何去除图例中的背景?
theme(legend.background = element_rect())
由于某种原因不起作用... 如何修改我的图例headers?我通过这个例子想象:http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/ 那应该是这样的:
scale_shape_discrete(name ="modified A4", breaks=c("1", "2"), labels = c("one","two"))
但我不知道如何让它工作?
我确定我完全误解了散点图中两个变量的显示,但我找不到更正它的方法?
谢谢!
根据@user20650 和@inscaven 的建议以及更多google 搜索,我希望更好地理解 ggplot 的组织方式以及如何生成我的情节:
# dummy data
a1<-c(seq(1,10,1))
a2<-c(seq(11,20,1))
a3<-c(rep(c(1,2),each = 5))
a4<-c(rep(c(5,10,15,20,25),2))
# create data frame
df<-data.frame(a1,a2,a3,a4)
# set nice theme
t1<-theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(size=.4))
# create scatter plot
ggplot(df, aes(x= a1, y= a2)) + # create basic plot
geom_point(aes(size = factor(a4), colour = factor(a3))) + # colour has to be inside of aes !! (ASSIGNED = MAPPED)
scale_colour_grey(name = "Set second\nline in title") + # change title of 1st legend, change colours
scale_size_discrete(name = "Name by size") + # change title of 2nd legend, size of point has been already assigned
theme(legend.key = element_blank()) + # delete grey boxes around the legend
labs(x = "x label", y = "y label") + # set labels on x and y axes
t1 # add nice theme
这导致: