我如何强制ggplot在图例上显示更多级别? [英] How can I force ggplot to show more levels on the legend?

查看:155
本文介绍了我如何强制ggplot在图例上显示更多级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个复杂的ggplot图,但有些事情不能按预期工作。



我提取了有问题的部分,创建了点和与其相关的图例。

  library(data.table)
library(ggplot2)
lev < - c(A,B,C,D)#定义等级。
bb < - c(40,30,20,10,5)/ 100#定义中断。
ll< -c(40%,30%,20%,10%,5%)#标签。
#创建数据
nodos< - data.table(event = c(A,B,D,C,D),ord = c(1, ),
NP = c(0.375,0.25,0.125,0.125,0.125))


ggplot()+ geom_point(data = nodos, aes(x = ord,
y = event,size = NP),color =black,shape = 16)+
ylim(lev)+ scale_size_continuous(name =Prop。,
= bb,labels = ll,range = c(0,6))+
scale_x_continuous(limits = c(0.5,4.5),
breaks = seq(1,4,1))



正如你所看到的,无论我使用什么样的休息和标签,我都不能强制ggplot绘制包含0%或10%的图例。
scale_size_continuous不断创建两个元素。

更小的点非常缩放。



我也尝试过scale_scale_area,但它也不起作用。



我使用R 3.4.2和ggplot2 2.2.1(也尝试了最新的github版本)。



我该如何得到它?

解决方案

如果您将限制以包含间歇,您将可以更改图例。目前大部分的中断超出了比例的默认限制。

  ggplot()+ 
geom_point(data = nodos,
aes(x = ord,y = event,size = NP),color =black,shape = 16)+
scale_size_continuous (name =Prop。,
breaks = bb,
limits = c(.05,.4),
labels = ll,
range = c(0,6 ))


I'm trying to create a complex ggplot plot but some things don't work as expected.

I have extracted the problematic part, the creation of points and its associated legend.

library(data.table)
library(ggplot2)
lev <- c("A", "B", "C", "D") # define levels.
bb <- c(40, 30,20,10,5)/100 # define breaks.
ll <- c("40%","30%","20%","10%","5%") # labels.
# Create data
nodos <- data.table(event = c("A", "B", "D", "C", "D"), ord = c(1, 2, 3, 3, 4),
NP = c(0.375, 0.25, 0.125, 0.125, 0.125))


ggplot() + geom_point(data=nodos,aes(x=ord, 
 y=event, size=NP), color="black", shape=16) +
 ylim(lev)  + scale_size_continuous(name="Prop.",
 breaks=bb, labels=ll, range=c(0,6))+ 
 scale_x_continuous(limits=c(0.5, 4.5),
 breaks=seq(1,4,1))

As you can see, no matter what breaks and labels I use I'm not able to force ggplot to paint a legend containing 0% or 10%. scale_size_continuous keeps creating just two elements.
And the smaller points are very badly scaled.

I have also tried with scale_scale_area, but it doesn't work either.

I'm using R 3.4.2 and ggplot2 2.2.1 (also tried the latest github version).

How can I get it?

解决方案

If you set the limits to encompass the breaks you'll be able to alter the legend. Current most of the breaks are outside the default limits of the scale.

ggplot() + 
    geom_point(data = nodos,
               aes(x = ord, y = event, size = NP), color="black", shape = 16) +
    scale_size_continuous(name = "Prop.",
                          breaks = bb,
                          limits = c(.05, .4),
                          labels = ll,
                          range = c(0, 6) )

这篇关于我如何强制ggplot在图例上显示更多级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆