发出“na”将条放置在R和ggplot2中的条形图中 [英] Issue with "na" in arranging the bars in a bar plot in R and ggplot2

查看:248
本文介绍了发出“na”将条放置在R和ggplot2中的条形图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot2创建一个降序条形图,并在R中绘制。我使用的数据是a12,它由ggplot在#Final Data frame is consumed below行下面消耗。 p>

如果您看到下面的快照,那么列a1中的na值应该出现在图中的第二个点上,但它不会按照降序排列并且被推到最后。

解决这个问题的一种方法是在那里硬编码值,但我不想那样做。取而代之的是,有没有一种方法可以让我们使用na按顺序而不用硬编码任何值?注意:请不要修改上面的数据#Final Data frame is consumed below以下的数据。我正在处理的实际数据实际上是相同的格式。请帮忙。

  a1 = c(A,,B,C,D,F )
b1 = c(165,154,134,110,94,78)
a12 = data.frame(a1,b1)
a12 [2,1] =NA

#最终数据帧在
pp1 <-ggplot(a12,
aes(x =重排序(a1,-b1),
y = b1,
文本= paste(User:< br>,a1,< br> Days:< br>,round(b1))))+
geom_bar(stat =identity,fill = #3399ff)+
scale_y_continuous(name =Time)+
scale_x_discrete(name =Employee)

ggplotly(pp1,tooltip =text,height = 392)

以下插件脚本

  a1 = c(A,NA,B,C,D,F)
b1 = c (b1,b1,stringsAsFactors = FALSE)
pp1 <-ggplot(a12,aes(x = reorder(a1,-b1), y = b1,text = paste(User:
< br>,a1,< br> Days:< br>,round(b1)))+
geom_bar统计=identity,fill =#3399ff)+ scale_y_continuous(名称
=Time)+ scale_x_discrete(name =Employee)
ggplotly(pp1,tooltip =text,height = 392)

解决方案

为了按照你想要的方式排列条,不应该有一个缺失值,即 NA ,在 df $ a1 中。我们以某种方式需要替换缺少的值。



由于您在上述评论中要求没有替换,没有修改数据,所以我建议您替换调用 ggplot ,例如 tidyr :: replace_na 。这将使您的原始数据不变。

  library(tidyr)
library(ggplot2)
pp1< ;< - ggplot(data = replace_na(a12,replace = list(a1 =NA)),
aes(x = reorder(a1,-b1),
y = b1,
text = paste(User:< br>,a1,< br> Days:< br>,round(b1))))+
geom_bar(stat =identity,fill =#3399ff)+
scale_y_continuous(name =Time)+
scale_x_discrete(name =Employee)

ggplotly(pp1,tooltip =text,身高= 392)



我希望这有助于。

I am trying to create a Descending bars bar plot using ggplot2 and plotly in R. The data I am using is "a12" which is consumed by the ggplot under the line "#Final Data frame is consumed below".

If you see the snap shot below, the "na" value in the column "a1" should come at second spot in the plot, however it does not follow the descending order and is pushed at the last.

One way to cure this issue is to hardcode the value there, but I don't want to do that. Instead is there a way using which I can make "na" follow the order without hard coding any value?

Note: Please do not modify the data above the line "#Final Data frame is consumed below". The actual data I am working on is actually in the same format. Please help.

a1 = c("A","","B","C","D","F")
b1 = c(165,154,134,110,94,78)
a12 = data.frame(a1,b1)
a12[2, 1] = "NA"

#Final Data frame is consumed below
pp1 <<- ggplot(a12 , 
               aes(x = reorder(a1, -b1), 
                   y = b1,
                   text = paste("User: <br>", a1, "<br> Days: <br>", round(b1)))) + 
  geom_bar(stat = "identity", fill = "#3399ff" ) + 
  scale_y_continuous(name = "Time") + 
  scale_x_discrete(name = "Employee") 

ggplotly(pp1, tooltip="text", height = 392)

Addon Script below:

a1 = c("A",NA,"B","C","D","F")
b1 = c(165,154,134,110,94,78)
a12 = data.frame(a1,b1,stringsAsFactors = FALSE)
pp1 <<- ggplot(a12 , aes(x = reorder(a1,-b1), y = b1,text=paste("User: 
<br>",a1, "<br> Days: <br>", round(b1)))) + 
geom_bar(stat = "identity", fill = "#3399ff" ) + scale_y_continuous(name 
="Time") + scale_x_discrete(name ="Employee") 
ggplotly(pp1, tooltip="text",height = 392)

解决方案

In order to arrange the bars in the way you want, there should not be a missing value, i.e. NA, in df$a1. We somehow need to replace that missing value.

Since you asked for "no replacement, no modification to the data" in the comments above, I suggest you replace the missing values inside the call to ggplot using, e.g. tidyr::replace_na. This will leave your original data unchanged.

library(tidyr)
library(ggplot2)
pp1 <<- ggplot(data = replace_na(a12, replace = list(a1 = "NA")),
                      aes(x = reorder(a1, -b1),
                          y = b1,
                          text = paste("User: <br>", a1, "<br> Days: <br>", round(b1)))) +
  geom_bar(stat = "identity", fill = "#3399ff") +
  scale_y_continuous(name = "Time") +
  scale_x_discrete(name = "Employee")

ggplotly(pp1, tooltip = "text",height = 392)

I hope this helps.

这篇关于发出“na”将条放置在R和ggplot2中的条形图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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