如何使用R在Barplot中的文本和条形之间绘制线条

时间:2017-09-16 17:15:52

标签: r

我正在绘制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

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我使用segment(x0,y0,x1,y1)方法在任何地方绘制线条。