【问题标题】:How to draw lines between a text and a bar in Barplot using R如何使用 R 在 Barplot 中的文本和条形之间绘制线条
【发布时间】:2017-09-16 17:15:52
【问题描述】:

我正在 R 中绘制条形图。我是 R 的新手。

我需要像下面的pdf文件那样实现代码的输出

https://drive.google.com/file/d/0B3XlF206d5UrUFcwbTY5eU9mbDA/view?usp=sharing

如果你看到,从 pdf 文件中,有线条连接栏和文本。我无法在 R 中实现相同的效果。谁能帮我解决这个问题。

以下是我的代码。

# Reading from the csv file 
hotdogs <-read.csv("hot-dogcontest-winners.csv",sep=",", header=TRUE)

# Colouring the bar based on conditions. 
fill_colors <- c()
for ( i in 1:length(hotdogs$New.record) ){
    if (hotdogs$New.record[i] == "1") {
        fill_colors <- c(fill_colors, "#31D331")
    } 
   else 
   {
        fill_colors <- c(fill_colors, "#D5D8D4")
   }
   }

# Output to a pdf file.
pdf("my_plot.pdf", width=25, height=10)

# Plotting the bar graph.
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col=fill_colors,space=0.5,xlab="Year",border=NA,ylab="Hot dogs and buns (HDB) eaten"
,main="Nathan's Hot Dog Eating Contest Results from 1980-2017")


axis(1, at = seq(1980,2016, by = 2),col="black",tcl=0)

# Making the yaxis not visible.
axis(2, col = "white", tcl = 0)

# Creating texts and placing it in certain co-ordinates.
text(10,65,"Nathan's hot dog eating contest every July 4th has been going on since the early 1900s,but it wasn't
until 2001,when things got serious.Takeru Kobayashi from Japan raised the bar, more than doubling 
the previous world record.Highlighted bars indicate new records.")

text(30,65,"Matthew Stonnie won in 2015 , after Joey Chestnut won 8 consecutive times")


text(10,25,"Frank Dellarosa sets a new 
world record with 21 and half HDBs")

text(20,55,"In 2001,Takeru Kobayashi, gets his first
win in the competition.He went on to win five
more years in a row.")

目前能够做到这一点。

https://drive.google.com/file/d/0B3XlF206d5UrcThwekxxc09MeFk/view?usp=sharing

csv 数据存在于该位置

https://drive.google.com/file/d/0B3XlF206d5UrdXFranV6YlNKYUE/view

感谢任何帮助。

【问题讨论】:

  • 我们无法运行您的代码,因为我们没有数据文件。你也可以发布数据吗?运行dput(hotdogs) 并将输出粘贴到您的问题中可能是最简单的。
  • 做一个更简单的例子。 (因为这是基本图形,所以可以使用lines 函数。)

标签: r


【解决方案1】:

我使用segment(x0, y0, x1, y1) 方法在各处绘制线条。

【讨论】:

    猜你喜欢
    • 2015-03-26
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    相关资源
    最近更新 更多