如何用颜色订购geom_segment ggplot

时间:2020-10-18 21:01:51

标签: r ggplot2

我是predictlda <- getFromNamespace("predict.lda", "MASS") predictlda(z) #$class # [1] v s s s s c s v s v v v v c v v c v c s s s s c c v c v v c s s v c s s c v s c v v s c s c s c c s v c s s c s s c c c s c s v #[65] v v v s c s c v v s s #Levels: c s v #$posterior # c s v #107 3.513603e-03 1.352029e-37 9.964864e-01 #37 2.749629e-26 1.000000e+00 5.088976e-50 # .. 库的新手。并尝试使用以下ggplot绘制图:

data.frame

使用以下代码段:

library(tidyverse)
df <-tribble(~event, ~startdate,~enddate,~loc,
      "A",as.POSIXct("1984/02/10"),as.POSIXct("1987/06/10"),"1",
      "B",as.POSIXct("1984/02/11"),as.POSIXct("1990/02/12"),"2",
      "A",as.POSIXct("1992/05/15"),as.POSIXct("1999/06/15"),"3",
      "C",as.POSIXct("2003/08/29"),as.POSIXct("2015/08/29"),"4",
      "B",as.POSIXct("2002/04/11"),as.POSIXct("2012/04/12"),"5",
      "E",as.POSIXct("2000/02/10"),as.POSIXct("2005/02/15"),"6")
max_date = max(df$startdate,df$enddate)

我可以输出以下图表:enter image description here

如何使用颜色(即ggplot(NULL)+ geom_segment(data = df,aes(x=loc, xend =loc,y = startdate, yend = enddate,colour=event),size = 5,alpha=0.6) + geom_label(aes(label=df$event,x = df$loc,y=max_date), size=2) + #geom_point(data=final_df,aes(x=newspaper,y=date),color="black") + Point from other data frame coord_flip() + xlab("LoC") + ylab("Year") 字段)来订购上述图表(换句话说,如何在event字段上进行分组操作,以便第一个应该显示首先是所有事件A,然后是事件B,C 等)?我尝试使用event包中的scale_x_continuousreorder,但没有用。如何在x轴上显示更多“年份”?我尝试使用tidyverse(此处提到R: ggplot display all dates on x axis,但它需要scale_x_dateas.Date ggplot需要geom_segment格式)。请随时纠正我!

任何帮助都会很棒!谢谢!

2 个答案:

答案 0 :(得分:2)

两个选项。我还颠倒了您的x和y,因此您不必使用coord_flip()并进行了其他一些小的修改,包括x轴标签(您一直在寻找scale_y_datetime,因为您将轴和“日期”实际上在POSIXct中)。另外,与Duck的答案不同的是facet_grid中我的秤=“免费”。您可能会决定标签,而鉴于这些新图形,您的“ loc”变量可能没有任何意义。

library(tibble); library(ggplot2)
df <-tribble(~event, ~startdate,~enddate,~loc,
             "A",as.POSIXct("1984/02/10"),as.POSIXct("1987/06/10"),"1",
             "B",as.POSIXct("1984/02/11"),as.POSIXct("1990/02/12"),"2",
             "A",as.POSIXct("1992/05/15"),as.POSIXct("1999/06/15"),"3",
             "C",as.POSIXct("2003/08/29"),as.POSIXct("2015/08/29"),"4",
             "B",as.POSIXct("2002/04/11"),as.POSIXct("2012/04/12"),"5",
             "E",as.POSIXct("2000/02/10"),as.POSIXct("2005/02/15"),"6")
max_date = max(df$startdate,df$enddate)

ggplot(df)+
  geom_segment(aes(y=event, yend = event, x = startdate, xend = enddate, colour=event),size = 5,alpha=0.6) +
  geom_label(aes(label=event, y = event, x=max_date), size=2) +
  xlab("Year") + ylab("LoC") +
  scale_x_datetime(date_breaks = "year", date_labels = "%Y") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))


ggplot(df)+
  geom_segment(aes(y=loc, yend = loc, x = startdate, xend = enddate, colour=event),size = 5,alpha=0.6) +
  geom_label(aes(label=event, y = loc, x=max_date), size=2) +
  xlab("Year") + ylab("LoC") +
  scale_x_datetime(date_breaks = "year", date_labels = "%Y") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  facet_grid(rows = vars(event), scales = "free")

reprex package(v0.3.0)于2020-10-18创建

答案 1 :(得分:1)

将其视为一个选项,如 @ArthurYip 所述,设置重新排序可能会影响您的情节感。您可以避免使用标签,而可以通过以下方式使用def scrapeFanCollection(url): browser = getBrowser() setattr(threadLocal, 'browser', browser) #Go to url browser.get(url) try: #Click show more button browser.find_element_by_class_name('show-more').click() #Wait two seconds time.sleep(2) #Scroll to the bottom loading full collection scroll(browser, 2) except Exception: pass #Return full album collection soup_a = BeautifulSoup(browser.page_source, 'lxml', parse_only=SoupStrainer('a', {"class": "item-link"})) #Empty array urls = [] # Looping through all the a elements in the page source for item in soup_a.find_all('a', {"class": "item-link"}): url = item.get('href') if(url != None): urls.append(url) return urls

facet_grid()

输出:

enter image description here