如何使用ggplot2清理数据并创建图形?

因此,我想使用来自Wikipedia的数据创建图,我在发现的表外创建了数据框。它包含两列-啤酒样式和范围(IBU),例如“ 20-50”。两者都是角色,因此我无法从中得出有意义的图表。我设法将IBU列更改为两个分别为数字(最小值和最大值)的列,但它在我的第一个数据帧内创建了第二个数据帧,试图找到类似的情况,但我不能,我现在被卡住了并且不不知道下一步该怎么做:( 预先为粘贴这么多代码感到抱歉,我只希望有人读取数据并查看其结构。

library(xml2)
library(rvest)
library(ggplot2)
library(tidyr)

file_html <- read_html(
  "https://pl.wikipedia.org/wiki/International_Bittering_Units",encoding = "UTF-8")
table_html <- html_node(file_html,"#mw-content-text > div > table")
table_IBU <- html_table(table_html,fill = TRUE)


table_IBU$IBU2 <- str_replace(table_IBU$`Stopień IBU`,"\\+","")
table_IBU$IBU3 <- tidyr::separate(table_IBU,IBU2,into = c("min","max"),sep = " – ")
table_IBU <- subset(table_IBU,select = -c(IBU2,`Stopień IBU`,`Gatunek piwa`))

table_IBU$IBU3$min2 <- as.numeric(table_IBU$IBU3$min)
table_IBU$IBU3$max2 <- as.numeric(table_IBU$IBU3$max)

#graph that I can come up with on my own

IBUgraph <- ggplot(table_IBU$IBU3,aes(reorder(`Gatunek piwa`,+ max2),max2)) + 
  geom_point(width = 0.5,color = "darkolivegreen",fill = "darkseagreen4") + 
  theme(text=element_text(size = 9)) 
IBUgraph = IBUgraph +
  labs(y = "Międzynarodowe Jednostki Goryczy (IBU)",x = "Gatunek",title = "Skala IBU - International Bitterness Units,czyli międzynarodowe jednostki goryczy")
IBUgraph <- IBUgraph + theme(axis.text.x=element_text(angle=45,hjust=1.1))

IBUgraph

最后,我想使用ggplot()在x轴上展示啤酒的样式创建一个图形,并为每种样式显示两个点,以显示最小的有效值,最大值。

Shuzhenabc123 回答:如何使用ggplot2清理数据并创建图形?

例如,您可以执行此操作,它称为哑铃图

ggplot(table_IBU$IBU3,aes(x=`Gatunek piwa`)) + 
      geom_point(aes(y=min2)) + # add point for min
      geom_point(aes(y=max2)) + # add point for max
      geom_segment(aes(xend=`Gatunek piwa`,y=min2,yend=max2)) + # create segment between min and max
      theme(axis.text.x = element_text(angle = 90,hjust = 1)) # rotate x axis

enter image description here

,

那么,您是否正在寻找类似的东西?

library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)
library(rvest)

#Acquire table
table_IBU <- read_html("https://pl.wikipedia.org/wiki/International_Bittering_Units",encoding = "UTF-8") %>%
  html_node(.,"#mw-content-text > div > table") %>%
  html_table(.,fill = TRUE)

#Extract scores into min and max values
table_IBU$IBU2 <- str_replace(table_IBU$`Stopień IBU`,"\\+","")
table_IBU %<>% separate(.,IBU2,into = c("min","max"),sep = " – ") %>% select(-c(`Stopień IBU`))
table_IBU$min <- as.integer(table_IBU$min)
table_IBU$max <- as.integer(table_IBU$max)
table_IBU %<>% gather(data = .,key = "Limit",value = "Value",min,max)

#Plot
table_IBU %>% ggplot(data = .,aes(x = `Gatunek piwa`)) + 
  geom_point(aes(y = Value,col = Limit)) + 
  xlab("Type of beer") +
  ylab("Score (0-120)") +
  theme(axis.text.x = element_text(angle = 45,hjust = 1))

RPlot

一种奇怪的方式来显示此数据。

本文链接:https://www.f2er.com/3148581.html

大家都在问