0
votes

enter image description here[![enter image description here][2]][2]

I want to have a flipped barchart with a color gradient to distinguish the bars. Below are the first 20 rows of my dataset bb_count is my dataset and the code to generate the barplot. The barplot is plotted correctly but the color gradient is not being applied, instead I get the error:

ERROR while rich displaying an object: Error: Discrete value supplied to continuous scale

bb_count <- bb %>% 
             count(chord, sort = TRUE)

# chord n
# C:maj 1183
# G:maj 1140
# A:maj 1071
# D:maj 1054
# F:maj 859
# E:maj 839
# Bb:maj    718
# B:maj 503
# Ab:maj    375
# Eb:maj    360
# A:min 328
# E:min 298
# Db:maj    293
# D:min 250
# B:min 236
# N 201
# E:min7    186
# C:min 176
# D:7   176
# A:min7    170

# Creating a bar plot from `bb_count`
bb_count %>%
  slice(1:20) %>%
  mutate(share = n/sum(n)) %>%
  mutate(chord = reorder(chord, share)) %>%
  ggplot(aes(x = chord, y = share, fill = chord)) +
  geom_bar(stat = 'identity', width = 0.95) + 
  coord_flip() + 
  scale_fill_gradient(low = "green", high = "red") +
  xlab("Share of total chords") +
  ylab("Chord")
1
dput(bb_count) is shorter and easier to reproduce than showing the console output. Better still, dput us the output of your pipeline just before the ggplot() line, i.e. store the result from everything up to the mutate(chord.. line. Then you'd see that the output from reorder(chord, ...) is categorical not numerical.smci
Also your image link [2] is missing, can you fill it in or else delete that line?smci
Also your code still isn't reproducible since you didn't supply chord. (Use dput not console. We need to see if it's categorical).smci

1 Answers

1
votes
Error: Discrete value supplied to continuous scale

Your x is discrete, so you need scale_x_discrete

df %>%
  slice(1:20) %>%
  mutate(share = n/sum(n)) %>%
  mutate(chord = reorder(chord, share)) %>%
  ggplot(aes(x = chord, y = share, fill = chord)) +
  geom_bar(stat = 'identity', width = 0.95) + 
  coord_flip() + 
  scale_x_discrete() +
  xlab("Share of total chords") +
  ylab("Chord")

enter image description here

data:

library(data.table)
df <- fread("
# chord n
# C:maj 1183
            # G:maj 1140
            # A:maj 1071
            # D:maj 1054
            # F:maj 859
            # E:maj 839
            # Bb:maj    718
            # B:maj 503
            # Ab:maj    375
            # Eb:maj    360
            # A:min 328
            # E:min 298
            # Db:maj    293
            # D:min 250
            # B:min 236
            # N 201
            # E:min7    186
            # C:min 176
            # D:7   176
            # A:min7    170
")