I'm trying to numerically order my bar graph using plotly and I'm also using the shiny widget, select box, which displays the bar graph of each type of organization. (Ex. type of organizations are medical, web, gaming, military, etc) On the y-axis for the bar graph is the name of the organization and the x-axis has the number of records. Here is my coding for numerically ordering all of my bar graphs:
df <- original.df %>% choice(type_org == input$choice)
df$abbrv <- ifelse(nchar(df$name) > 20, abbreviate(df$name), df$name)
df$abbrv <- factor(df$abbrv, levels = unique(df$abbrv)[order(df$number, decreasing = FALSE)])
plot_ly(
x = df$number,
y = df$abbrv,
type = 'bar',
text = ifelse(nchar(df$name) > 20, df$name, "")) %>%
layout(margin = list(l = 150))
A bit explanation about my dataframe and coding. Basically, I abbreviated organization names that are longer than 20 character, which is why I have an abbrv column in my dataset. So in the y-axis, the full organization name doesn't show if it's longer than 20 characters, but instead it shows the abbreviation, where I used the abbreivation
function. More details about it is in my previous post.
Anyways, the problem that I am having right now is that the factor()
function is ordering 98% of the bar graphs. However, it doesn't sort two bar graphs for some reason. I have no idea why it's sorting everything else EXCEPT the bar graph for two type of organization. military and telecom. Here is the dataframe for the type of organizations, telecom:
structure(list(entity_name = c("KDDI", "T-Mobile, Deutsche Telecom",
"AT&T", "AT&T", "KT Corp.", "TerraCom & YourTel", "Vodafone",
"Three", "Bell"), year = c(2006, 2006, 2008, 2010, 2012, 2013, 2013, 2017, 2017
), type_org = c("telecoms", "telecoms", "telecoms", "telecoms",
"telecoms", "telecoms", "telecoms", "telecoms", "telecoms"),
records_lost = c(4000000L, 17000000L, 100000L, 100000L, 8700000L, 180000L,
2000000L, 200000L, 1900000L),
abbreviation = structure(c(6L, 8L, 1L, 1L, 2L, 7L,
3L, 5L, 4L), .Label = c("AT&T", "KT Corp.", "Vodafone", "Bell",
"Three", "KDDI", "TerraCom & YourTel", "T-DT"), class = "factor")), .Names = c("entity_name",
"alt_name", "description", "year", "type_org", "leak_method",
"interesting", "records_lost", "data_sensitivity", "source_1",
"source_2", "source_3", "source_name", "abbreviation"), row.names = c(NA,
9L), class = "data.frame")
Here is the dataframe for military:
structure(list(entity_name = c("US Dept of Defense", "US National Guard",
"US Military", "US Military", "US Army", "Stratfor", "Tricare"
), year = c(2009, 2009, 2009, 2010, 2011, 2011, 2011), type_org = c("military",
"military", "military", "military", "military", "military", "military"
), records_lost = c(72000L, 130000L, 76000000L, 300000L, 50000L, 900000L, 4900000L), abbreviation = structure(c(2L,
3L, 6L, 6L, 4L, 1L, 5L), .Label = c("Stratfor", "US Dept of Defense",
"US National Guard", "US Army", "Tricare", "US Military"), class = "factor")), .Names = c("entity_name",
"alt_name", "description", "year", "type_org", "leak_method",
"interesting", "records_lost", "data_sensitivity", "source_1",
"source_2", "source_3", "source_name", "abbreviation"), row.names = c(NA,
7L), class = "data.frame")
For some reason, these two are not sorted numerically like the other type of organizations and I'm not sure what to do. I tried using the arrange()
dplyr function as well, but that doesn't do anything. I don't understand why it sorts all the other bar graphs though. Would anyone happen to know how to fix this?