简体   繁体   English

ggplot2 错误 - '离散值提供给连续比例'

[英]ggplot2 error - 'Discrete value supplied to continuous scale'

I'm trying to plot filled rectangles using the following code -我正在尝试使用以下代码绘制填充矩形 -

tplot.old <- ggplot() + 
geom_rect(data=t.df,mapping=aes(xmin=xa, xmax=xb, ymin=ya, ymax=yb, fill=Train)) +
ylab("Cars per week") + xlab("Miles") + ggtitle(old.title) +
theme(axis.text.x = element_text(angle=45,size=5,hjust=1), axis.text=element_text(size=5), axis.text.y = element_text(size=5), title=element_text(size=5), legend.text=element_text(size=5)) + 
scale_fill_manual(values=color_values) + 
scale_x_continuous(breaks=plot.stns$Avg_Miles,labels=plot.stns[,"City_Name"]) + 
coord_cartesian(xlim=c(0,1700),ylim=c(0,500))

t.df looks like this: t.df看起来像这样:

   Train  xa   xb ya        yb       col
a  MHKNP 407 1594  0 22.806452       red
b  MPDNP 407 1594  0  9.258065 darkgreen
cd MHKRO 407 1594  0  5.258065    orange

and plot.stns :plot.stns

      Avg_Miles       City_Name
1           0          EUGENE
4          15    BLAKESLEEJCT
12         64     LONGVIEWJCT
25        118          ALBINA
45        306          HINKLE

This seems to work when I plot this interactively on the console, but inside a for loop gives this error -当我在控制台上以交互方式绘制此图时,这似乎有效,但在 for 循环中会出现此错误 -

Error: Discrete value supplied to continuous scale错误:提供给连续刻度的离散值

color_values :颜色值:

     MHKNP      MHKNPB       MHKPC       MHKRO       MPCGR       MPSHK 
     "red"     "green"      "blue"    "maroon"    "orange"     "black" 
     QCONP      QPCNPP       MPDNP 
   "magenta"   "skyblue" "darkgreen" 

This is somehow related to scale_x_continuous (it does not give the error when I remove it), but I can't figure out what is wrong here.这在某种程度上与scale_x_continuous相关(当我删除它时它不会给出错误),但我无法弄清楚这里出了什么问题。 Any ideas?有任何想法吗?

I did a read.table(pipe("pbpaste")) (I'm on a Mac) on your two partial data frames and moderately restructured your code.我在你的两个部分数据帧上做了一个read.table(pipe("pbpaste")) (我在 Mac 上)并适度地重组了你的代码。 If the data frame column types aren't the same as yours, perhaps paste a dput vs ascii tabular output.如果数据框列类型与您的不同,请粘贴dput与 ascii 表格输出。

However, I don't get the same error you're getting:但是,我没有得到与您相同的错误:

t.df <- structure(list(Train = structure(c(1L, 3L, 2L), .Label = c("MHKNP", 
                  "MHKRO", "MPDNP"), class = "factor"), xa = c(407L, 407L, 407L
                  ), xb = c(1594L, 1594L, 1594L), ya = c(0L, 0L, 0L), yb = c(22.806452, 
                  9.258065, 5.258065), col = structure(c(3L, 1L, 2L), .Label = c("darkgreen", 
                  "orange", "red"), class = "factor")), .Names = c("Train", "xa", 
                  "xb", "ya", "yb", "col"), class = "data.frame", row.names = c("a", 
                  "b", "cd"))

plot.stns <- structure(list(Avg_Miles = c(0L, 15L, 64L, 118L, 306L), City_Name = structure(c(3L, 
                        2L, 5L, 1L, 4L), .Label = c("ALBINA", "BLAKESLEEJCT", "EUGENE", 
                        "HINKLE", "LONGVIEWJCT"), class = "factor")), .Names = c("Avg_Miles", 
                        "City_Name"), class = "data.frame", row.names = c("1", "4", "12", 
                        "25", "45"))

color_values = t.df$col

gg <- ggplot(data=t.df, mapping=aes(xmin=xa, xmax=xb, ymin=ya, ymax=yb, fill=Train))
gg <- gg + geom_rect()
gg <- gg + labs(y="Cars per week", x="Miles", title="(old.title)")
gg <- gg + scale_fill_manual(values=color_values) 
gg <- gg + scale_x_continuous(breaks=plot.stns$Avg_Miles,
                              labels=plot.stns[,"City_Name"]) 
gg <- gg + coord_cartesian(xlim=c(0, 1700), ylim=c(0,500))
gg <- gg + theme(axis.text.x = element_text(angle=45, size=5, hjust=1), 
                 axis.text = element_text(size=5), 
                 axis.text.y = element_text(size=5), 
                 title = element_text(size=5),
                 legend.text = element_text(size=5))
gg

在此处输入图片说明

"old.title" was missing, so I just made it a text string. “old.title”丢失了,所以我只是把它变成了一个文本字符串。 I've also found structuring ggplot code this way makes it much easier to rearrange, modify and debug.我还发现以这种方式构建 ggplot 代码可以更轻松地重新排列、修改和调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM