I have the following data:
Text refers to a narrative, mi is the count of a particular feature, and shi is the count of another feature. I have them in a data frame as follows. I'm not sure how manually recreate this dataframe with vectors of the values, so I apologize in advance that this isn't as reproducible as it otherwise could be.
test <- as.data.frame(read_excel("plotTEST.xlsx"))
I want a plot in which the x axis is each text, and y shows the counts of both features. I want to approximate something like this, as shown here or here .
Using those examples I cobbled together the following:
ggplot(data=test, aes(x=text, y=mi, fill=shi)) + geom_bar(stat="identity")
And through lots of tweaking and several iterations, this is the cleanest result I have gotten:
Gradient seems to be default despite the fact that I tried following the code in the linked examples and they don't give specific details about the color selections.
It's a gradient rather than discreet colors, how can I get discreet colors for the mi counts and shi counts?
I tried this solution mentioned here but that just results in another separate key with different colors and each bar gets a different colored outline.
You can do it like this:
# Your data
text <- c("Swollen River", "Anaconda caught", "Chikwan speaks", "Lake explodes","Swollen River", "Anaconda caught", "Chikwan speaks", "Lake explodes")
values <- c(5,18,45,98, 0,9,33,23)
type <- c("mi","mi","mi","mi","shi","shi","shi","shi")
df <- data.frame("text"=text, "values"=values, "type"=type)
# dataframe in long format:
# text values type
# 1 Swollen River 5 mi
# 2 Anaconda caught 18 mi
# 3 Chikwan speaks 45 mi
# 4 Lake explodes 98 mi
# 5 Swollen River 0 shi
# 6 Anaconda caught 9 shi
# 7 Chikwan speaks 33 shi
# 8 Lake explodes 23 shi
# Plot
ggplot(data=df, aes(x=text, y=values, fill=type)) + geom_bar(stat="identity")
In other words, you do need the data preferably in long format, as described here .
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.