To fix this, include the position="dodge" argument in your geom_bar(), like so. However, you can see the bar graph has stacked the species ontop of one another. ggplot(weeds.summarise2, aes(x=soil, y=mean, fill=species)) + ggplot also plots our legend automatically, which is handy. We plot multiple columns by specifying one column in our x axis, and filling/colouring by another. Summarise(mean = mean(flowers), se=sd(flowers)/sqrt(n())) weeds.summarise2 % group_by(species, soil) %>% To plot multiple columns, for example a soil by species interaction, is quite simple.įirstly, we will run our summarise command, adding the soil column into our group_by() command to generate the means and standard error for the soil, species combinations. In the last examples, we plotted a single column graph with errorbars and significant notation. doi: 10.Built with the "Learn" Theme using Hugo and Blogdown Library ( plotly ) y % layout ( yaxis = list ( showgrid = FALSE, showline = FALSE, showticklabels = TRUE, domain = c ( 0, 0.85 )), xaxis = list ( zeroline = FALSE, showline = FALSE, showticklabels = TRUE, showgrid = TRUE )) fig1 % add_annotations ( xref = 'x1', yref = 'y', x = x_saving * 2.1 + 3, y = y, text = paste ( round ( x_saving, 2 ), '%' ), font = list ( family = 'Arial', size = 12, color = 'rgb(50, 171, 96)' ), showarrow = FALSE ) fig2 % layout ( yaxis = list ( showgrid = FALSE, showline = TRUE, showticklabels = FALSE, linecolor = 'rgba(102, 102, 102, 0.8)', linewidth = 2, domain = c ( 0, 0.85 )), xaxis = list ( zeroline = FALSE, showline = FALSE, showticklabels = TRUE, showgrid = TRUE, side = 'top', dtick = 25000 )) fig2 % add_annotations ( xref = 'x2', yref = 'y', x = x_net_worth, y = y, text = paste ( x_net_worth, 'M' ), font = list ( family = 'Arial', size = 12, color = 'rgb(128, 0, 128)' ), showarrow = FALSE ) fig % layout ( title = 'Household savings & net worth for eight OECD countries', legend = list ( x = 0.029, y = 1.038, font = list ( size = 10 )), margin = list ( l = 100, r = 20, t = 70, b = 70 ), paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)' ) fig % add_annotations ( xref = 'paper', yref = 'paper', x = -0.14, y = -0.15, text = paste ( 'OECD (2015), Household savings (indicator), Household net worth (indicator). Library ( plotly ) y organized', 'The course developed myabilities and skills forthe subject', 'The course developed myability to think critically aboutthe subject', 'I would recommend thiscourse to a friend' ) x 1 agree', 'Agree', 'Neutral', 'Disagree', 'Stronglydisagree' ) fig % add_trace ( x = ~ x 2, marker = list ( color = 'rgba(71, 58, 131, 0.8)' )) fig % add_trace ( x = ~ x 3, marker = list ( color = 'rgba(122, 120, 168, 0.8)' )) fig % add_trace ( x = ~ x 4, marker = list ( color = 'rgba(164, 163, 204, 0.85)' )) fig % add_trace ( x = ~ x 5, marker = list ( color = 'rgba(190, 192, 213, 1)' )) fig % layout ( xaxis = list ( title = "", showgrid = FALSE, showline = FALSE, showticklabels = FALSE, zeroline = FALSE, domain = c ( 0.15, 1 )), yaxis = list ( title = "", showgrid = FALSE, showline = FALSE, showticklabels = FALSE, zeroline = FALSE ), barmode = 'stack', paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)', margin = list ( l = 120, r = 10, t = 140, b = 80 ), showlegend = FALSE ) # labeling the y-axis fig % add_annotations ( xref = 'paper', yref = 'y', x = 0.14, y = y, xanchor = 'right', text = y, font = list ( family = 'Arial', size = 12, color = 'rgb(67, 67, 67)' ), showarrow = FALSE, align = 'right' ) # labeling the percentages of each bar (x_axis) fig % add_annotations ( xref = 'x', yref = 'y', x = x 1 / 2, y = y, text = paste ( data, '%' ), font = list ( family = 'Arial', size = 12, color = 'rgb(248, 248, 255)' ), showarrow = FALSE ) fig % add_annotations ( xref = 'x', yref = 'y', x = x 1 + x 2 / 2, y = y, text = paste ( data, '%' ), font = list ( family = 'Arial', size = 12, color = 'rgb(248, 248, 255)' ), showarrow = FALSE ) fig % add_annotations ( xref = 'x', yref = 'y', x = x 1 + x 2 + x 3 / 2, y = y, text = paste ( data, '%' ), font = list ( family = 'Arial', size = 12, color = 'rgb(248, 248, 255)' ), showarrow = FALSE ) fig % add_annotations ( xref = 'x', yref = 'y', x = x 1 + x 2 + x 3 + x 4 / 2, y = y, text = paste ( data, '%' ), font = list ( family = 'Arial', size = 12, color = 'rgb(248, 248, 255)' ), showarrow = FALSE ) fig % add_annotations ( xref = 'x', yref = 'y', x = x 1 + x 2 + x 3 + x 4 + x 5 / 2, y = y, text = paste ( data, '%' ), font = list ( family = 'Arial', size = 12, color = 'rgb(248, 248, 255)' ), showarrow = FALSE ) # labeling the first Likert scale (on the top) fig % add_annotations ( xref = 'x', yref = 'paper', x = c ( 21 / 2, 21 + 30 / 2, 21 + 30 + 21 / 2, 21 + 30 + 21 + 16 / 2, 21 + 30 + 21 + 16 + 12 / 2 ), y = 1.15, text = top_labels, font = list ( family = 'Arial', size = 12, color = 'rgb(67, 67, 67)' ), showarrow = FALSE ) fig
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |