3

não consigo anterar o meu código para refletir o que preciso. Cada coluna deve conter os valores nos intervalos horários corretos, por exemplo: a primeira coluna deveria conter valores correspondentes aos horários entre 0h <= x > 1h, a segunda coluna deveria conter valores correspondentes aos horários entre 1h <= x > 2h a terceira coluna deveria conter valores correspondentes aos horários entre 2h <= x > 3h . . . a vigésima quarta coluna deveria conter valores correspondentes a 23h <= x > 0h

No entanto, como podem ver na imagem, a primeira coluna está exibindo valores antes de 0h, o que parece estar relacionado ao intervalo entre 23h e 0h. Eu gostaria que essa coluna fosse movida para o final do gráfico, já que esse intervalo corresponde às horas finais do dia, e não ao início.

Alguém poderia me auxiliar?

# Criar uma nova coluna para cores com base no dia da semana
consumo_per_capita_agrupado <- consumo_per_capita_agrupado %>%
  mutate(COR = case_when(
    DIA_DA_SEMANA %in% c("Saturday", "Sunday") ~ "darkblue",  # Azul escuro para sabado e domingo
    TRUE ~ "lightblue"  # Azul claro para os outros dias
  ))

# Grafico para os dias da semana
grafico_dias_da_semana <- ggplot(consumo_per_capita_agrupado, aes(x = HORA, y = MEDIA_CONSUMO_PER_CAPITA_NORMALIZADO, fill = COR)) +
  geom_bar(stat = "identity", color = "black", width = 0.94, position = "identity") +  # Barras verticais encostadas
  labs(x = "Período (hora)", y = "Consumo normalizado médio (adimensional)", title = "") +
  scale_x_continuous(breaks = seq(0, 23, by = 1)) +  # Escala de hora em hora
  geom_vline(xintercept = seq(0, 23, by = 1), linetype = "dashed", color = "gray") +  # Linhas verticais a cada hora
  facet_wrap(~ DIA_DA_SEMANA, nrow = 2) +  
  scale_fill_identity() +  
  theme_minimal() +  
  theme(axis.text.x = element_text(angle = 90, hjust = 0.5),  # Rotacionando os labels do eixo X e centralizando
        axis.title.x = element_text(margin = margin(t = 5), vjust = 1, size = 14), 
        axis.title.y = element_text(margin = margin(r = 5), vjust = 1, size = 14), 
        axis.text.y = element_text(margin = margin(r = 5), size = 12),  
        strip.text = element_text(size = 16, face = "bold"),  
        panel.grid.major = element_blank(),  
        panel.grid.minor = element_blank())  
grafico_dias_da_semana 



> dput(consumo_per_capita_agrupado)
structure(list(DIA_DA_SEMANA = structure(c(5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("Monday", 
"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
), class = "factor"), HORA = c(0L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 0L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 0L, 1L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
0L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L), MEDIA_CONSUMO_PER_CAPITA = c(4.065, 2.53291666666667, 
3.79055555555556, 8.10166666666667, 2.18796296296296, 8.4279012345679, 
3.99133333333333, 5.83583333333333, 2.39760416666667, 2.75625, 
6.50833333333333, 3.7074358974359, 2.54988888888889, 3.01083333333333, 
6.96416666666667, 3.435, 4.1075, 12.5218181818182, 5.3855303030303, 
4.49660256410256, 3.33782051282051, 4.9375, 7.3694, 2.35, 4.57055555555556, 
4.29611111111111, 8.30425925925926, 6.07533333333333, 1.81788461538462, 
3.022, 4.8437037037037, 5.43875, 7.26083333333333, 4.94462962962963, 
9.08318181818182, 10.4362962962963, 12.6911111111111, 5.23964285714286, 
5.60358024691358, 3.71888888888889, 1.2, 1.28333333333333, 1.53333333333333, 
3.785, 6.08173913043478, 4.71320512820513, 8.66117647058823, 
7.92666666666667, 3.19522222222222, 4.62803418803419, 10.4936666666667, 
5.34140350877193, 5.26583333333333, 1.65851851851852, 7.0605, 
7.244, 8.22714285714286, 10.1077272727273, 6.43604166666667, 
4.57283950617284, 8.49833333333333, 1.14, 6.010625, 3.22533333333333, 
2.545625, 5.41114035087719, 4.46833333333333, 3.2425, 2.43518518518519, 
2.04416666666667, 2.27958333333333, 3.78011904761905, 3.35733333333333, 
10.1533333333333, 9.32272727272727, 12.8088888888889, 5.64666666666667, 
3.771, 4.85142857142857, 17.4883333333333, 1.1, 7.9, 1.33333333333333, 
3.12039215686275, 6.29585585585586, 2.58109375, 4.0680303030303, 
3.57026315789474, 3.11359649122807, 2.67969696969697, 2.25424528301887, 
2.94302083333333, 4.4836231884058, 3.12071428571429, 5.48466666666667, 
5.81541666666667, 2.13346153846154, 9.08526315789474, 2.67089743589744, 
4.58011904761905, 6.86365384615385, 5.70321428571429, 2.92375, 
1.28333333333333, 3.29177083333333, 6.92911764705882, 3.52433333333333, 
2.22090277777778, 3.91566666666667, 2.52862745098039, 5.87257575757576, 
3.83060283687943, 7.7565625, 2.425, 3.81, 2.52583333333333, 1.9884375, 
6.8731746031746, 9.60485507246377, 11.856, 4.56358024691358, 
4.53246913580247, 5.29597222222222, 8.63166666666667, 4.3, 7.2, 
2.26666666666667, 3.23047619047619, 9.3596, 2.51629166666667, 
3.66695402298851, 8.83492753623188, 9.99283950617284, 6.9221875, 
4.75138888888889, 4.905625, 5.78705128205128, 12.5810606060606, 
2.62016666666667, 3.98933333333333, 13.1233333333333, 10.6808333333333, 
11.6206790123457, 7.35538461538462, 5.85217948717949, 5.06115942028986
), MEDIA_CONSUMO_PER_CAPITA_NORMALIZADO = c(0.0447465463151522, 
0.0238710268546528, 0.0388029208844823, 0.054611903721368, 0.0166572618767823, 
0.0465916997979699, 0.027125774260437, 0.0457989735404157, 0.0227339520046635, 
0.0155488284861506, 0.0571473542744915, 0.0273163640952588, 0.0254852012190441, 
0.0323394536689525, 0.049514198449795, 0.0281331561094675, 0.0381899927588119, 
0.0654779177440122, 0.0362677289457464, 0.0347411159476889, 0.0367764567085153, 
0.0368308289059443, 0.0453309837800838, 0.0193018203066595, 0.0234389187631167, 
0.022326018366961, 0.0424387131295359, 0.033770358184721, 0.0107300993812654, 
0.0246742165620341, 0.0263993532150524, 0.0329975703227708, 0.0386355592587534, 
0.0270476391728562, 0.0570983068594374, 0.0629580546758795, 0.101702601716567, 
0.0375803213015188, 0.0274924288271817, 0.0461829218730514, 0.00542078872475945, 
0.00579723238620108, 0.00692656337052597, 0.0214489999318043, 
0.0283525080331749, 0.0266272398252505, 0.0430204404133998, 0.0374586222266983, 
0.0200510583272898, 0.0203347430204615, 0.0437986121637833, 0.0207875598113754, 
0.026975160535858, 0.00847324517105913, 0.0353054674323844, 0.0338208806952059, 
0.0393527986950763, 0.0473261158563354, 0.0357607832040672, 0.0246083418656499, 
0.0489585160455428, 0.0261946446156972, 0.0290530499745382, 0.0323147917599683, 
0.030162712035818, 0.0353011752905329, 0.0272729100976793, 0.0136047772392836, 
0.0196379700183656, 0.0103564507397962, 0.0197012170175156, 0.0176215904726336, 
0.0238747063921232, 0.0640066912792547, 0.0459044843660687, 0.0716864922067422, 
0.0346210402179801, 0.0326191921248927, 0.0346658626481942, 0.108822779879706, 
0.00510600479737911, 0.0370569916415231, 0.00696160673883532, 
0.0202987263752074, 0.0335944366946987, 0.020649702508392, 0.0257030490845119, 
0.0240559387420263, 0.0148672912061275, 0.0160640544506635, 0.0183790148479191, 
0.0199624787075139, 0.0228502354715945, 0.0183986958476279, 0.0340892362685732, 
0.0338363617937259, 0.0165467286352761, 0.0579342283129788, 0.0212262894621368, 
0.0294999539053181, 0.0385512436487227, 0.047379411156144, 0.016420949171581, 
0.00773278701695188, 0.025609511057406, 0.0392602191981789, 0.0272996664396684, 
0.0158704814354962, 0.038030880781769, 0.0270181862877949, 0.0409374401013857, 
0.0217242819814321, 0.0448551329311796, 0.0148942326618099, 0.0275704951380411, 
0.0172978505813532, 0.0152182858413152, 0.0516750911705899, 0.0643470507658489, 
0.0674044899516916, 0.0351657072907227, 0.0346968185727795, 0.0340170555973892, 
0.0379023534451992, 0.0184450402144772, 0.0709534368070953, 0.010250380620751, 
0.0146301330904344, 0.0538502162850684, 0.0151763018542177, 0.0164860865534787, 
0.0332076372070612, 0.041595176250691, 0.0248703104857991, 0.0288467690179088, 
0.0300577672385581, 0.0304878225397067, 0.0720007389534413, 0.0134770588517693, 
0.0157438925133906, 0.0447912582351363, 0.0564469838471523, 0.0596956389337103, 
0.0362409634942647, 0.0301467968772538, 0.0243458469178361), 
    COR = c("lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "darkblue", "darkblue", "darkblue", "darkblue", 
    "darkblue", "darkblue", "darkblue", "darkblue", "darkblue", 
    "darkblue", "darkblue", "darkblue", "darkblue", "darkblue", 
    "darkblue", "darkblue", "darkblue", "darkblue", "darkblue", 
    "darkblue", "darkblue", "darkblue", "darkblue", "darkblue", 
    "darkblue", "darkblue", "darkblue", "darkblue", "darkblue", 
    "darkblue", "darkblue", "darkblue", "darkblue", "darkblue", 
    "darkblue", "darkblue", "darkblue", "darkblue", "darkblue", 
    "darkblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue", "lightblue", "lightblue", "lightblue", 
    "lightblue", "lightblue")), row.names = c(NA, -146L), class = c("tbl_df", 
"tbl", "data.frame"))

Imagem exemplo com a barra deslocada

1 Resposta 1

4

Adicione o argumento just = 0 dentro do geom_bar. Por padrão, just = 0.5, o que deixa as barras centralizadas nos valores do eixo x. Colocando just = 0 elas ficam alinhadas à esquerda e colocando just = 1 elas ficam alinhadas à direita.

# Criar uma nova coluna para cores com base no dia da semana
consumo_per_capita_agrupado <- consumo_per_capita_agrupado %>%
  mutate(COR = case_when(
    DIA_DA_SEMANA %in% c("Saturday", "Sunday") ~ "darkblue",  # Azul escuro para sabado e domingo
    TRUE ~ "lightblue"  # Azul claro para os outros dias
  ))

# Grafico para os dias da semana
grafico_dias_da_semana <- ggplot(consumo_per_capita_agrupado, aes(x = HORA, y = MEDIA_CONSUMO_PER_CAPITA_NORMALIZADO, fill = COR)) +
  geom_bar(stat = "identity", color = "black", width = 0.94, position = "identity", just = 0) +  # Barras verticais encostadas
  labs(x = "Período (hora)", y = "Consumo normalizado médio (adimensional)", title = "") +
  scale_x_continuous(breaks = seq(0, 23, by = 1)) +  # Escala de hora em hora
  geom_vline(xintercept = seq(0, 23, by = 1), linetype = "dashed", color = "gray") +  # Linhas verticais a cada hora
  facet_wrap(~ DIA_DA_SEMANA, nrow = 2) +  
  scale_fill_identity() +  
  theme_minimal() +  
  theme(axis.text.x = element_text(angle = 90, hjust = 0.5),  # Rotacionando os labels do eixo X e centralizando
        axis.title.x = element_text(margin = margin(t = 5), vjust = 1, size = 14), 
        axis.title.y = element_text(margin = margin(r = 5), vjust = 1, size = 14), 
        axis.text.y = element_text(margin = margin(r = 5), size = 12),  
        strip.text = element_text(size = 16, face = "bold"),  
        panel.grid.major = element_blank(),  
        panel.grid.minor = element_blank())  
grafico_dias_da_semana 

inserir a descrição da imagem aqui

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .