问题描述
我正在尝试为ggplot2的某些数据添加辅助y轴。我的数据p
如下所示:
aspect yhat Count
<dbl> <dbl> <dbl>
1 1.37 0.329 144
2 16.9 0.329 5
3 32.3 0.330 5
4 47.8 0.331 0
5 63.3 0.333 57
6 78.8 0.333 67
7 94.3 0.332 13
8 110. 0.332 0
9 125. 0.332 0
10 141. 0.331 0
然后我尝试像这样绘制它:
#get the information to for using a secondary y axis
ylim.prim <- c(min(p$yhat),max(p$yhat))
ylim.sec <- c(min(p$Count,na.rm = T),max(p$Count,na.rm = T))
b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])
#Now make plot
p1 = ggplot(p,aes(x = get(col),y = yhat)) +
geom_line() +
labs(x = col) +
geom_line(aes(y = a + Count*b),color = "red",linetype = 2) +
scale_y_continuous(name = "Mean Response",sec.axis = sec_axis(~ (. - a)/b,name = "Number of Pixels")) +
theme_light() +
theme(axis.line.y.right = element_line(color = "red"),axis.ticks.y.right = element_line(color = "red"),axis.text.y.right = element_text(color = "red"),axis.title.y.right = element_text(color = "red")
) +
theme(legend.title = element_blank()) +
theme(text=element_text(size=22))
这将返回该图:
我遇到的问题是黑线和红线之间有太多未使用的空白,这使得解释值有些困难,我想知道是否可以以某种方式使用更好的转换来将线条拉得更近,甚至重叠,但是我不知道怎么做。
我认为这可能与以下事实有关:相同的yhat
值可以具有不同的count
值。如果是这样,有办法解决吗?
解决方法
总的来说,我同意这样一个前提,即多个(不同的)轴很容易使观看者感到困惑。使用颜色和不同的对象(点与线)有助于区分两个值,但这是一种缓解措施。
这里是一个刺,使用点数(如建议的那样):
class ToDoList(FloatLayout):
def __init__(self,**kwargs):
super(ToDoList,self).__init__(**kwargs)
f = open('todolist_demo.json','r')
data = json.load(f)
self.to_dos = data
to_do_but = Button()
to_do_but.bind(on_press=ToDoList.CreateToDo)
self.add_widget(to_do_but)
def AddToDo(self):
self.to_dos.append(9)
def CreateToDo(self):
but = Button()
but.bind(on_press=AddToDo)
self.add_widget(but)
(使用count_slope_intercept <- c(m = diff(range(p$Count)),b = min(p$Count))
yhat_slope_intercept <- c(m = diff(range(p$yhat)),b = min(p$yhat))
p$Count2 <- yhat_slope_intercept["b"] +
yhat_slope_intercept["m"] * (p$Count - count_slope_intercept["b"]) / count_slope_intercept["m"]
ggplot(p,aes(x = aspect,y = yhat)) +
geom_line() +
labs(x = "aspect") +
geom_point(aes(y = Count2),color = "red") +
scale_y_continuous(
name = "Mean Response",sec.axis = sec_axis(~ count_slope_intercept["b"] + count_slope_intercept["m"] *
(. - yhat_slope_intercept["b"]) / yhat_slope_intercept["m"])
) +
theme_light() +
theme(axis.line.y.right = element_line(color = "red"),axis.ticks.y.right = element_line(color = "red"),axis.text.y.right = element_text(color = "red"),axis.title.y.right = element_text(color = "red")
) +
theme(legend.title = element_blank()) +
theme(text=element_text(size=22))
或类似的方法可能更平滑。)
编辑:是的,与scales
相同:
scales
(虽然我在上面很明确,同时包含了p$Count3 <- scales::rescale(p$Count,to = range(p$yhat),from = range(p$Count))
ggplot(p,y = yhat)) +
geom_line() +
labs(x = "aspect") +
geom_point(aes(y = Count3),sec.axis = sec_axis(~ scales::rescale(.,to = range(p$Count),from = range(p$yhat)))
) +
theme_light() +
theme(axis.line.y.right = element_line(color = "red"),axis.title.y.right = element_text(color = "red")
) +
theme(legend.title = element_blank()) +
theme(text=element_text(size=22))
和to=
两个参数,但是from=
的默认值为from
,因此可以将代码缩短一些。我认为最好将此示例明确,并显示range(x)
的位置和from
的位置。)