ad_s
ad_s

Reputation: 1700

GGPLOT2: how to plot specific selections inthe ggplot() script

This is a peak into a large dataset named P, where there are 10 concessionaries (CS) that have different shops (SHP) with several numeric values. The dataset lists them ordered by week (WK) 2 tm 52. It creates a large file. A peak into just the 6 first rows:

WK,MND,CS,SHP,RevCY,RevLY,TCY,TLY,ACY,ALY
=========================================
2,JAN,AAA,AAA Shop 1,16834,16686,1837,1983,2853,3002 

2,JAN,AAA,AAA Shop 2,95919,114696,9742,11813,20521,24673

2,JAN,BBB, BBB shop 1,93428,92212,7647,7857,18436,17984

2,JAN,BBB, BBB Shop 2,30600,35831,2748,3063,5579,6408

2,JAN,CCC, CCC Shop 1, 65229,78761,6074,7172,13852,16706

2,JAN,CCC, CCC Shop 2,465,754,73,118,92,162

I have difficulties plotting just the values that concern fi SHP==AAA.

p <- ggplot(P, aes(WK, RevCY)) + geom_bar(stat="identity")

This is plotting all shops and all CS. So the underlying question is to understand how I can plot only the shops (SHP) from CS=AAA. Let's say with the weeks (WK) on the x-axis and RevCY on the y-axis in the ggplot() + geom_bar(stat="identity") code.

Is this the right direction?:

p <- ggplot(P[P$CS=="AAA"], aes(WK, RevCY)) + geom_bar(stat="identity")

So without creating all kinds of subsets and straight into the ggplot() code. Hope my question is clear.

Upvotes: 3

Views: 39204

Answers (2)

Florin
Florin

Reputation: 304

Does this help you?

ggplot(t, aes(WK, RevCY)) + geom_bar(data=subset(t,CS=="AAA"),stat="identity")

Upvotes: 10

Sven Hohenstein
Sven Hohenstein

Reputation: 81733

To extract certain rows from your data frame t, you have to use

t[t$CS == "AAA", ]

instead of t[t$CS == "AAA"]. The latter syntax is used to select columns.

The plot command:

p <- ggplot(t[t$CS == "AAA", ], aes(WK, RevCY)) + geom_bar(stat = "identity")

I suppose you want to add some arguments to produce multiple bars per WK instead of a single stacked bar:

p <- ggplot(t[t$CS == "AAA", ], aes(as.factor(WK), RevCY)) + 
       geom_bar(stat = "identity", aes(group = RevCY), position = "dodge")

Upvotes: 4

Related Questions