本文介绍了更改数值的xlim会导致错误ggplot R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中使用ggplot通过以下代码生成了一个分组的barplot

I have a grouped barplot produced using ggplot in R with the following code

ggplot(mTogether, aes(x = USuniquNegR, y = value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_discrete(name = "Area", 
                    labels = c("Everywhere", "New York")) +
xlab("Reasons") +
ylab("Proportion of total complaints") +
coord_flip() +
ggtitle("Comparison between NY and all areas") 

mTogether是使用以下代码创建的

mTogether is created using the following code

mTogether <- melt(together, id.vars = 'USuniquNegR')

数据框共同由

      USperReasons      USperReasonsNY                 USuniquNegR
1    0.198343304187759   0.191304347826087                 Late Flight
2     0.35987114588127   0.321739130434783      Customer Service Issue
3   0.0667280257708237    0.11304347826087                Lost Luggage
4   0.0547630004601933 0.00869565217391304     Flight Booking Problems
5    0.109065807639208   0.121739130434783                  Can't Tell
6  0.00460193281178095                   0             Damaged Luggage
7   0.0846755637367694  0.0782608695652174            Cancelled Flight
8   0.0455591348366314  0.0521739130434783                  Bad Flight
9   0.0225494707777266  0.0347826086956522                   longlines
10  0.0538426138978371  0.0782608695652174 Flight Attendant Complaints

可以通过以下方式共同生成

Together can be generated by the following

together<-data.frame(cbind(USperReasons,USperReasonsNY,USuniquNegR))

其中

USperReasons <- c(0.19834,0.35987,.06672,0.05476,0.10906,.00460,.08467,0.04555,0.02254,0.05384)

USperReasonsNY <- c(0.191304348,0.321739130,0.113043478,0.008695652,0.121739130,0.000000000,0.078260870,0.05217391,0.034782609,0.078260870)

USuniquNegR <- c("Late Flight","Customer Service Issue","Lost Luggage","Flight Booking Problems","Can't Tell","Damaged Luggage","Cancelled Flight","Bad Flight","longlines","Flight Attendant Complaints")

问题是当我尝试使用

+ xlim(0, 1)

我似乎得到一个错误:离散值提供给连续刻度我不明白为什么会发生这种情况,但是我需要解决它,因为当前x轴从0以下开始并且高度压缩: ggplot输出的图像

I just seem to get an error:Discrete value supplied to continuous scaleI can't understand why this happens but I need to resolve it because currently the x axis starts below 0 and is very highly packed:image of ggplot output

推荐答案

您需要从

together<-data.frame(cbind(USperReasons,USperReasonsNY,USuniquNegR))

因为str(together)告诉所有三列都是因素.

because str(together) tells that all three columns are factors.

使用

together <- data.frame(USperReasons, USperReasonsNY, USuniquNegR)

该图对我来说看起来很合理(无需使用ylimxlim).

the plot looks reasonable to me (without having to use ylim or xlim).

因此,该错误不是在ggplot2之内,而是在数据准备中.因此,请提供一个完整的工作示例,该示例可以在下次提出问题时进行复制,粘贴和运行.谢谢.

So, the error was not within ggplot2 but in data preparation.Therefore, please, provide a full working example which can be copied, pasted and run when asking a question next time. Thank you.

这篇关于更改数值的xlim会导致错误ggplot R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:41