如何将情节与传说结合起来?
我正在尝试将图例附加到 R 中的图上。 我尝试了以下代码(取自 http://www.harding.edu/fmccown/r/ )
# Define cars vector with 5 values
cars <- c(1, 3, 6, 4, 9)
# Define some colors ideal for black & white print
colors <- c("white","grey70","grey90","grey50","black")
# Calculate the percentage for each day, rounded to one
# decimal place
car_labels <- round(cars/sum(cars) * 100, 1)
# Concatenate a '%' char after each value
car_labels <- paste(car_labels, "%", sep="")
# Create a pie chart with defined heading and custom colors
# and labels
pie(cars, main="Cars", col=colors, labels=car_labels,
cex=0.8)
# Create a legend at the right
legend(1.5, 0.5, c("Mon","Tue","Wed","Thu","Fri"), cex=0.8,
fill=colors)
然而,这并不能很好地工作。在 pie(cars, main="Cars", col=colors, labels=car_labels,cex=0.8) 行之后,显示的图没有图例:-) .......我在互联网上看到的每个示例似乎在绘图函数之后都有 legend 函数,所以看起来很奇怪..............
当我尝试执行图例时我得到的函数
图例(1.5, 0.5, c("星期一","星期二","星期三","星期四","星期五"), cex=0.8, + 填充=颜色) strwidth(图例,单位 =“用户”,cex = cex) 中的错误: plot.new尚未被调用
I'm trying to attach a legend to a plot in R.
I tried the following code ( taken from http://www.harding.edu/fmccown/r/ )
# Define cars vector with 5 values
cars <- c(1, 3, 6, 4, 9)
# Define some colors ideal for black & white print
colors <- c("white","grey70","grey90","grey50","black")
# Calculate the percentage for each day, rounded to one
# decimal place
car_labels <- round(cars/sum(cars) * 100, 1)
# Concatenate a '%' char after each value
car_labels <- paste(car_labels, "%", sep="")
# Create a pie chart with defined heading and custom colors
# and labels
pie(cars, main="Cars", col=colors, labels=car_labels,
cex=0.8)
# Create a legend at the right
legend(1.5, 0.5, c("Mon","Tue","Wed","Thu","Fri"), cex=0.8,
fill=colors)
However, this does not work really well. After the pie(cars, main="Cars", col=colors, labels=car_labels,cex=0.8) line , the plot is shown without a legend :-) .......Every example I see on the Internet seems to have the legend function after the plotting function so it seems very weird..............
When I try to execute the legend function I get
legend(1.5, 0.5, c("Mon","Tue","Wed","Thu","Fri"), cex=0.8,
+ fill=colors)
Error in strwidth(legend, units = "user", cex = cex) :
plot.new has not been called yet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你脱离了坐标系。请尝试此操作
,它会生成下面的图表:
有关不同的情况,请参阅
legend
的帮助页面放置选项。You are off the coordinate system. Try this instead
which produces the chart below:
See the help page for
legend
for different placement options.我认为
1.5, 0.5
的位置使其脱离了页面。尝试使用
pie
函数后,它会显示为没有图例;legend
函数将图例添加到当前绘图中。附言。您还可以考虑其他类型的图。饼图因视觉上的误导而臭名昭著。
I think the position
1.5, 0.5
puts it off the page. TryAfter the
pie
function it appears with no legend; thelegend
function adds the legend to the current plot.PS. You may also consider other types of plots. Pie charts are notorious for being visually misleading.