R:如何在ggplotly中修改传奇?
我的水平点图通过ggplotly可视化。 3个数值变量被放在图上。一切都很好:
library(ggplot2)
df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
Percentage = c(23,16,32,71,3,60,15,21,44,60),
rate = c(10,12,20,200,-25,12,13,90,-105,23),
change = c(10,12,-5,12,6,8,0.5,-2,5,-2))
plt <- ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
geom_segment(aes(x = (min(rate,change)-4), xend = (max(rate,change)+4),
y = origin, yend = origin), color = 'gray') +
geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
#geom_vline(xintercept =17, linetype = 1, color = 'black') +
geom_point(aes(fill = 'rate'), shape = 21, size = 10, color = NA) +
geom_text(aes(label = rate, color = 'rate')) +
geom_point(aes(x = change, fill = 'change'),
color = NA, shape = 21, size = 10) +
geom_text(aes(label = change, x = change, color = "change")) +
geom_point(aes(x = (max(rate,change)+5.5), fill = "Percentage"), color = "gray",
size = 10, shape = 21) +
geom_text(aes(x = (max(rate,change)+5.5), label = paste0(Percentage, "%")),size=3)+
theme_minimal(base_size = 16) +
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
scale_fill_manual(values = c('#aac7c4', '#5f9299','black')) +
scale_color_manual(values = c("black", "white")) +
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50')) +
labs(color = NULL, y = NULL, fill = NULL)+
theme(axis.title = element_text(size=15), legend.title = element_text(size=2))
plt <- ggplotly(plt)
#customize legend
plt$x$data[[3]]$name <- plt$x$data[[3]]$legendgroup <-
plt$x$data[[4]]$name <- plt$x$data[[4]]$legendgroup <- "rate"
plt$x$data[[5]]$name <- plt$x$data[[5]]$legendgroup <-
plt$x$data[[6]]$name <- plt$x$data[[6]]$legendgroup <- "change"
plt$x$data[[7]]$name <- plt$x$data[[7]]$legendgroup <-
plt$x$data[[8]]$name <- plt$x$data[[8]]$legendgroup <- "Percentage"
plt
但是,当我激活(删除#)geom_vline(xintercept = 17,lineType = 1,color ='black')
代码行,以便在图上添加垂直线,隐藏变量传说无法正常工作。例如,如果我们隐藏“更改”变量:“速率”的数量消失,而其中一些仍显示。我认为该解决方案应在plt $ x $ data
中找到。 此外,我想订购分类变量“起源从上到下以百分比下降,例如,如果 j 具有最高的百分比,那么它应该位于顶部,而且,我也不会可能是可能
的src =“ https://i.sstatic.net/h2hu7.png” alt =“在此处输入图像说明”>
I have horizontal dots plot visualized by ggPlotly. 3 numerical variables are put on plot. everything works nice:
library(ggplot2)
df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
Percentage = c(23,16,32,71,3,60,15,21,44,60),
rate = c(10,12,20,200,-25,12,13,90,-105,23),
change = c(10,12,-5,12,6,8,0.5,-2,5,-2))
plt <- ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
geom_segment(aes(x = (min(rate,change)-4), xend = (max(rate,change)+4),
y = origin, yend = origin), color = 'gray') +
geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
#geom_vline(xintercept =17, linetype = 1, color = 'black') +
geom_point(aes(fill = 'rate'), shape = 21, size = 10, color = NA) +
geom_text(aes(label = rate, color = 'rate')) +
geom_point(aes(x = change, fill = 'change'),
color = NA, shape = 21, size = 10) +
geom_text(aes(label = change, x = change, color = "change")) +
geom_point(aes(x = (max(rate,change)+5.5), fill = "Percentage"), color = "gray",
size = 10, shape = 21) +
geom_text(aes(x = (max(rate,change)+5.5), label = paste0(Percentage, "%")),size=3)+
theme_minimal(base_size = 16) +
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
scale_fill_manual(values = c('#aac7c4', '#5f9299','black')) +
scale_color_manual(values = c("black", "white")) +
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50')) +
labs(color = NULL, y = NULL, fill = NULL)+
theme(axis.title = element_text(size=15), legend.title = element_text(size=2))
plt <- ggplotly(plt)
#customize legend
plt$x$data[[3]]$name <- plt$x$data[[3]]$legendgroup <-
plt$x$data[[4]]$name <- plt$x$data[[4]]$legendgroup <- "rate"
plt$x$data[[5]]$name <- plt$x$data[[5]]$legendgroup <-
plt$x$data[[6]]$name <- plt$x$data[[6]]$legendgroup <- "change"
plt$x$data[[7]]$name <- plt$x$data[[7]]$legendgroup <-
plt$x$data[[8]]$name <- plt$x$data[[8]]$legendgroup <- "Percentage"
plt
However when I activate (remove #) geom_vline(xintercept =17, linetype = 1, color = 'black')
code line, in order to add vertical line on plot, hiding variables from the legend does not work properly. For instance if we hide 'change' variable: numbers of 'rate' are disappeared while some of them are still shown. I think the solution should be found in plt$x$data
.
In addition, I want to order the categorical variable "origin descending from top to down by percentage, for example, if J has the highest percentage it should be on the top and also, I don't if that's possible but I want to keep A always on the bottom in ranking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按百分比订购
Origin
列很简单。通过将Origin
转换为其级别由百分比
的值确定的因素,这是在数据级别完成的:您自定义的传说发生了奇怪的事情是在之前,您在一些现有层之前添加了一层,从而丢弃了您使用的索引来修改传奇组。最简单的解决方法是在现有层之后绘制线路:
现在您可以完全像以前一样自定义传奇组:
如果您希望该行在所有点和文本后面,请保持您现有的绘图代码原样,并递增传奇分组代码中的所有索引:
Ordering the
origin
column by the percentages is straightforward. This is done at the data level, by convertingorigin
to a factor whose levels are determined by the value ofPercentage
:The reason that strange things were happening with your customized legend is that you added a layer before some of your existing layers, which throws off the indexing you are using to modify the legend groups at the end. The easiest fix for this is to draw the line after your existing layers:
Now you can customize the legend groups exactly as before:
If you want the line to be behind all the points and text, then keep your existing plotting code as it was, and increment all the indices in your legend grouping code:
如果您仍然想在底部(尽管@Allan Cameron的答案看起来很棒!),这将按百分比排序,并且将保持底部。
保持您的情节和数据与您的问题一样,我开始构建情节。
为了通过百分比(除“ A”)重新排序该值,我通过
百分比
添加了行号,并订购了该数据以匹配图中的订单。然后,我用它在图中重新排序y轴。我在此代码中留下了支票和余额,因此看起来很多,但其中很多是验证。现在是时候重新排序y轴了。由于
ggplot&lt; - &gt;,必须更改一些事情。 Plotly
翻译。range
(或者它将隐藏A并在顶部添加无用的空格),并且type
需要与订单一起更改。使用参数categoryarray
设置订单。我注意到重新订购后,我注意到垂直线不再可见。我再次创建了行。 (这是由于y轴的变化。)
现在,对于传奇名称。有很多方法可以做到这一点。我注意到您的正确性,添加了一层,然后遇到了问题。您可以使用Regex,而不是摘录。另一个选择是检查以找到更改的轨迹。
现在,您只需要致电情节即可。
If you still wanted A at the bottom (although @Allan Cameron's answer looks great!), this will order by percentages and it will keep A at the bottom.
Keeping your plot and data as it is in your question, I started with building the plot.
To reorder the values by percentages, other than 'A', I reordered the data by
Percentage
added row numbers, and ordered it to match the order in the plot. I then used this to reorder the y-axis in the graph. I left my checks and balances in this code, so it may look like a lot, but a lot of it is the validation.Now it's time to reorder the y-axis. There are a few things that have to be changed due to the
ggplot <-> plotly
translation.range
(or it will hide the A and add useless whitespace at the top) and thetype
need to be changed, along with the order. The order is set with the parametercategoryarray
.I noticed after I added the reorder, that the vertical lines were no longer visible. I created the lines again. (This is due to the changes in the y-axis.)
Now for the legend names. There are a lot of ways to do this. I noticed that you had it right, added a layer, and then had a problem. Instead of picking through, you could use regex. Another option is to inspect to find the traces to change.
Now you just have to call the plot.