gizzard
gizzard

Reputation: 135

Shiny R ggplot: Scatterplot axis has gone weird

The y-axis should be able to run from 0-535,000 and the lines should be proportionate in their positions. I don't possess the vocabulary to adequately describe this issue, so here are two pictures.

See what I mean?

A closer look

server.R snippet

   carbon_emissions <- read.csv("/Users/mathewsayer/Documents/Work/Level 7/Shiny Flat Tax/Carbon/Carbon_Emissions/data/carbon_emissions0513.csv")

output$carbonPlot <- renderPlot({
 pCarbon <- ggplot(data=carbon_emissions[carbon_emissions$category%in%input$emission_choose,],
 aes(x=year, y=co2, group=category, colour=category))+
 geom_line()+
 geom_point()
 pCarbon + labs(title="UK Domestic, Industry and Transport CO2 Emissions")  
 })

ui.R snippet

checkboxGroupInput(
    "emission_choose",
    label = "Plot CO2 emissions from various sectors",
    choices = c(
      "Industrial and Commericial Electricity" = "i_c_electric",
      "Industrial and Commericial Gas" = "i_c_gas",
      "Large Industrial Installations" =
        "large_industry",
      "Other Industrial and Commericial Fuels" = "i_c_other_fuel",
      "Agriculture" =
        "agriculture",))

 plotOutput("carbonPlot")

data sample

year,co2,category
2005,"110,579.1",i_c_electric
2006,"116,665.8",i_c_electric
2007,"113,343.8",i_c_electric
2008,"112,510.3",i_c_electric
...
2010,7.6,pc_emission
2011,6.9,pc_emission
2012,7.1,pc_emission
2013,7.0,pc_emission

Thanks in advance good people.

Upvotes: 2

Views: 279

Answers (1)

Tutuchan
Tutuchan

Reputation: 1567

Looks like your co2 variable is a character instead of a numeric.

Just do the following just after reading the file.

carbon_emissions$co2 <- as.numeric(gsub(",", "", carbon_emissions$co2))

Upvotes: 3

Related Questions