R 在ggvis中的图表下方添加水平离散图例
我想在R 在ggvis中的图表下方添加水平离散图例,r,plot,ggvis,legend-properties,R,Plot,Ggvis,Legend Properties,我想在ggvis图表下方做一个水平图例。我可以使用图例属性将其放置在图表下方,但不知道如何将标签水平放置在图例标题下方。以下是最低可复制代码(从web上抓取) df1=data.frame(x=sample(1:10),y=sample(1:10)) df2=数据帧(x=1:10,y=1:10) df3=数据帧(x=1:10,y=sqrt(1:10)) df2$id% #x和y的值必须介于0和1之间 #例如,对于x轴,0是最左边的点,1是最右边的点 添加图例(“笔划”,title=“圆柱体”,
ggvis
图表下方做一个水平图例。我可以使用图例属性将其放置在图表下方,但不知道如何将标签水平放置在图例标题下方。以下是最低可复制代码(从web上抓取)
df1=data.frame(x=sample(1:10),y=sample(1:10))
df2=数据帧(x=1:10,y=1:10)
df3=数据帧(x=1:10,y=sqrt(1:10))
df2$id%
#x和y的值必须介于0和1之间
#例如,对于x轴,0是最左边的点,1是最右边的点
添加图例(“笔划”,title=“圆柱体”,
属性=图例\道具(
图例=列表(
x=标度值(“x_rel”,0.2),
y=标度值(y相对值,-.2)
))) %>%
图层点(x=~x,y=~y,数据=df1,笔划:=‘黑色’)
这无疑是一种黑客行为,但效果很好:
df4 %>% ggvis(x=~x,y=~y,stroke=~id) %>% layer_lines() %>%
#make sure you use add relative scales
add_relative_scales() %>%
#values for x and y need to be between 0 and 1
#e.g for the x-axis 0 is the at far-most left point and 1 at the far-right
add_legend("stroke", title = "Cylinders", values = c(1, 1),
properties = legend_props(
legend = list(
x = scaled_value("x_rel", 0.2),
y = scaled_value("y_rel", -.2)
))) %>%
add_legend("stroke", title = " ", values = c(2, 2),
properties = legend_props(
legend = list(
x = scaled_value("x_rel", 0.23),
y = scaled_value("y_rel", -.2)
))) %>%
layer_points(x=~x,y=~y,data = df1,stroke:='black')
基本上,我正在添加另一个add_图例
,将标题设置为空白,调整scale_值
,使其非常接近第一个图例,但不重叠。然后,我将第一个图例设置为values=c(1,1)
,第二个图例设置为values=c(2,2)
,以便两个值相互叠加。这使得它看起来像一个具有水平值的图例。 现在是休眠状态,也许你可以考虑切换到代码> GoGoLaveS//C>。
下面是一个类似的图,您可以通过稍微处理示例数据来获得:
df5 <- df4[df4$id==1,]
colnames(df5)[2] <- "y1"
library(tidyverse)
df5 <- df5 %>%
mutate(
y0 = df1[order(df1$x),c(2)],
y2 = sqrt(x)
)
df5 <- df5[, c(1,4,2,5)]
library(googleVis)
plot_df5 <- gvisLineChart(df5, options=list(
legend="bottom",
series =
"[{labelInLegend: 'Dot', color: 'black'},
{labelInLegend: 'Cylinders: 1', color: 'blue', curveType: 'dot'},
{labelInLegend: 'Cylinders: 2', color: 'orange'}]"
)
)
plot(plot_df5)
df5
由于这似乎仍然是一个悬而未决的问题,我怀疑如果没有重大的黑客攻击,这是不可能的。但是我可能错了。@Felix知道这个功能是否已经添加到了ggvis
中了吗?我的答案和你想要的一样吗?
df5 <- df4[df4$id==1,]
colnames(df5)[2] <- "y1"
library(tidyverse)
df5 <- df5 %>%
mutate(
y0 = df1[order(df1$x),c(2)],
y2 = sqrt(x)
)
df5 <- df5[, c(1,4,2,5)]
library(googleVis)
plot_df5 <- gvisLineChart(df5, options=list(
legend="bottom",
series =
"[{labelInLegend: 'Dot', color: 'black'},
{labelInLegend: 'Cylinders: 1', color: 'blue', curveType: 'dot'},
{labelInLegend: 'Cylinders: 2', color: 'orange'}]"
)
)
plot(plot_df5)