简体   繁体   中英

How to plot point to point excluding zero in ggplot

I want to plot the frequency of parts of speech in a text. I have a facet plot for each POS tag and in each graph the x-axis is the sentence index and the y axis the frequency for that POS tag.

The plots seem to plot from point to x-axis rather than point to point. How can I change this?

library(udpipe)
library(dplyr)
library(stringr)
library(ggplot2)
library(gutenbergr)


ud_model <- udpipe_download_model(language = "english")
ud_model <- udpipe_load_model(ud_model$file_model)
txt<-gutenberg_download(152)
txt<-paste(unlist(txt), collapse =" ")


# Annotate (POS tag) the text

x <- udpipe_annotate(ud_model, txt)
x <- as.data.frame(x)


#Need to get frequency per sentence of each term

heatBySentence<-x%>%select(sentence_id,upos)%>% group_by(sentence_id,upos) %>%summarise(Number=n())


ggplot(data=heatBySentence[19:nrow(heatBySentence),], aes(x=sentence_id, y=Number,group=upos)) +
  geom_point()+
  geom_line()+
  geom_smooth()+
  facet_wrap(~ upos, scales = "free")

Your problem is mostly scale, I checked your data, and you have a lot of ones in your dataset, which means that a lot of your observations will be ones. The problem will look a lot clearer if we see a subset of your data

heatBySentence<- x %>% select(sentence_id,upos) %>% group_by(sentence_id,upos) %>%summarise(Number=n()) 

firstHundred <- heatBySentence %>% filter(sentence_id < 100)

If I do the same graph as you did here but only until sentence 99, this is what it will look like:

ggplot(data=heatBySentence[19:nrow(firstHundred),], aes(x=sentence_id, y=Number,group=upos)) +
  geom_point()+
  geom_line()+
  geom_smooth()+
  facet_wrap(~ upos, scales = "free")

在此处输入图片说明

As you can see there, all of your points are connected among them, but since you have a lot of ones in your data-set, and you have over 4000 sentences, they look crammed together and as if every point started from the origin. there was nothing wrong with your graph to begin with, only to many ones and a crammed x axis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM