Values ​​of groups that occur only once in the OTHER field

I have a collection of Roman coins that I am trying to present using ggplot in R.

There are 25 different items in the data, but I would like to combine all the items that have only one occurrence in the "OTHER" field, so the graphs are easier to read.

  Medium Method Denom Date Era 1 Silver Struck Denarius 112 BCE:111 BCE Period Vc. 119-91 BCE 2 Bronze Cast Χαλκα μεγεθους 181 BCE:174 BCE Period IIIc. 187-155 BCE 3 Bronze Struck Litra:Half-litra 269 BCE Period I – 269 - c. 222 BCE 4 Bronze Struck Litra:Half-litra 269 BCE Period I – 269 - c. 222 BCE 5 Silver Struck Didrachm 275 BCE:270 BCE Period I – 269 - c. 222 BCE 6 Bronze Struck Double-litra 275 BCE:270 BCE Period I – 269 - c. 222 BCE 

Using the data.frame sample above, the “Denom” column should have every value that happens only once, grouped together and displayed as “others”. I think I should do this in the data before starting the plot. Please point me in the right direction.

Here is the code I use for ggplot if that helps.

 ggplot(data=longbadian, aes(x=Era, fill=Denom)) + geom_bar(aes(x=Era2), data = longbadian, stat="bin") + theme(axis.text.x = element_text(angle=75, hjust=1), legend.title=element_blank()) + xlab("Sydenham Periods") + ylab("Coins by Denomination") 

Here is a graph example:

enter image description here

+6
source share
1 answer

Something like that:

 ## example data dd <- data.frame(DENOM=rep(LETTERS[1:7],c(10,5,4,rep(1,4)))) tt <- table(dd$DENOM) ## count occurrences singletons <- names(tt)[tt==1] ## find singletons tmpc <- as.character(dd$DENOM) ## convert from factor to char tmpc[tmpc %in% singletons] <- "OTHER" ## replace values dd$DENOM <- factor(tmpc) ## convert back to factor 

The only problem with this solution is that it will ruin any existing default order that does not have a default value in your DENOM factor.

+5
source

Source: https://habr.com/ru/post/988327/


All Articles