如何将情节与传说结合起来?

发布于 2025-01-07 13:17:46 字数 1203 浏览 5 评论 0原文

我正在尝试将图例附加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄凉少年不暖心 2025-01-14 13:17:46

你脱离了坐标系。请尝试此操作

# Create a legend at the right   
legend("topleft", c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, fill=colors)

,它会生成下面的图表:

pie Chart

有关不同的情况,请参阅 legend 的帮助页面放置选项。

You are off the coordinate system. Try this instead

# Create a legend at the right   
legend("topleft", c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, fill=colors)

which produces the chart below:

pie chart

See the help page for legend for different placement options.

莫多说 2025-01-14 13:17:46

我认为 1.5, 0.5 的位置使其脱离了页面。尝试

legend("right", c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, fill=colors)

使用 pie 函数后,它会显示为没有图例; legend 函数将图例添加到当前绘图中。

附言。您还可以考虑其他类型的图。饼图因视觉上的误导而臭名昭著。

I think the position 1.5, 0.5 puts it off the page. Try

legend("right", c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, fill=colors)

After the pie function it appears with no legend; the legend 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文