0

I was trying to implement a gapminder example in R using the plotly package from here which completely worked fine, but when I tried changing the data (here I used my own data set) and it shrank the values on Y-axis of the graph. Here is the code

 library(gapminder)
    library(plotly)
    library(ggplot2)
    gg <- ggplot(gapminder_test, aes(gdpPercap, lifeExp, color = continent)) +
      geom_point(aes(size = pop, frame = year, ids = country)) +
      scale_x_log10()
    ggplotly(gg)

this is how the graph looks like enter image description here As you can see here on Y-axis the values have been shrunk off, how can I make it proper like a similar of gapminder

Domnick
  • 509
  • 8
  • 25
  • Without knowing the changes you applied to your data set `gapminder_test` very difficult to diagnose. Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – mpalanco Jan 30 '18 at 11:23
  • The data is quite big to be reproduced, so I thought it to share it online, `dput` wouldn't also help. – Domnick Jan 30 '18 at 12:08
  • 1
    just double-check if **lifeExp** is numerical – MLavoie Jan 30 '18 at 12:40
  • @MLavoie I don't know, it might sound strange, but when I took the input from an excel file its working fine, previously the input was from csv file – Domnick Jan 30 '18 at 13:10
  • when you read gapminder_test what is the str()? – MLavoie Jan 30 '18 at 13:11
  • `> str(gapminder_test) Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 2025 obs. of 6 variables: $ country : chr "Shimla" "Shimla" "Shimla" "Shimla" ... $ year : num 1 2 3 4 5 6 7 8 9 10 ... $ pop : num NA NA NA NA NA NA NA NA NA NA ... $ continent: chr "Tier II" "Tier II" "Tier II" "Tier II" ... $ lifeExp : num NA NA NA NA NA NA NA NA NA NA ... $ gdpPercap: num NA NA NA NA NA NA NA NA NA NA ...` – Domnick Jan 30 '18 at 13:17

1 Answers1

1

I downloaded your file and imported it. To avoid the lifeExp column being coerced into a factor because of the blanks (empty cells in your Excel file) you can use na.strings = "N/A"

gapminder_test <- read.csv("gapminderData_share.csv", na.strings = "N/A")

If already imported, as MLavoie pointed out, coerce it to numeric with as.numeric:

gapminder_test$lifeExp <- as.numeric(gapminder_test$lifeExp)
gg <- ggplot(gapminder_test, aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year, ids = country)) +
  scale_x_log10()
ggplotly(gg)

Chart and animation look fine:

enter image description here

mpalanco
  • 12,960
  • 2
  • 59
  • 67
  • How do you scale the Y-axis, In your graph its showing(0,200,400,600) whereas in my graph its shows (5000,10000) ?? – Domnick Feb 01 '18 at 11:04
  • I did not tweak the axis scales. I read the file you posted. Maybe the comma separator is not right. But there is something definitely wrong in the data: in both life expectancy (above 100) and GDP. Once you solve it, the code should produce the right plot. – mpalanco Feb 01 '18 at 14:38