2

I have a plot and I would like to convert it into log log scale. I have tried different solution but none of them didn't create right log log plot.

The original plot looks like this enter image description here

and I used this script to plot it.

plot(ggplot(data=the.table[[i]],aes(x=the.table[[i]]$friends_count,y=the.table[[i]]$degree.in))+
labs(x="Friends Count",y="Degree In")+
       geom_point(color="#56B4E9")+ggtitle(paste(i,"Degree In-Friends Count",collapse="")))

The difference between values on the x and y axis are really huge so I would like to plot it with log log scale

enter image description here

I have searched stcakoverflow to find a solution and I changed the script like this.

plot(ggplot(data=the.table[[i]],aes(x=the.table[[i]]$friends_count,y=the.table[[i]]$degree.in))+
         scale_x_log10(limits = c(1, NA), 
                       labels = trans_format("log10", math_format(10^.x)),
                       breaks=trans_breaks("log10", function(x) 10^x, n=6)) +
         scale_y_log10(limits = c(1, NA),
                       labels = trans_format("log10", math_format(10^.x)),
                       breaks=trans_breaks("log10", function(x) 10^x, n=6)) +
         labs(x="Friends Count",y="Degree In")+
       geom_point(color="#56B4E9")+ggtitle(paste(i,"Degree In-Friends Count",collapse="")))

It produced plot with this warning messages:

Warning messages:
1: In scale$trans$trans(x) : NaNs produced
2: Removed 1 rows containing missing values (geom_point).

Do you have any suggestion? Thanks.

I have put sample data in case you need it:

structure(list(screen_name = structure(c(70L, 29L, 91L, 37L, 
99L, 33L, 46L, 26L, 63L, 48L, 80L, 17L, 15L, 89L, 88L, 82L, 69L, 
77L, 30L, 12L, 58L, 93L, 64L, 65L, 67L, 41L, 62L, 10L, 74L, 76L, 
1L, 9L, 47L, 35L, 24L, 14L, 71L, 72L, 34L, 23L, 39L, 11L, 52L, 
57L, 86L, 22L, 100L, 3L, 2L, 53L, 78L, 36L, 97L, 84L, 90L, 8L, 
45L, 38L, 50L, 55L, 96L, 61L, 13L, 16L, 43L, 60L, 85L, 32L, 20L, 
83L, 66L, 59L, 56L, 94L, 54L, 31L, 73L, 79L, 18L, 44L, 40L, 4L, 
27L, 19L, 92L, 95L, 75L, 21L, 25L, 6L, 28L, 87L, 7L, 51L, 81L, 
68L, 5L, 42L, 49L, 98L), .Label = c("_Anahtar_", "_fani__", "_uzumce", 
"28BeyzaTosun", "2cerkesoglu", "34fuzuli34RT", "abatila", "Adimozr", 
"AforizmaYazar", "ahmetfuadi", "Ak_ekip_rt", "Ak_Ekip_Rte", "ak_parti_tt", 
"AkgenclikBerlin", "AkGercekler", "AkkulisAK", "Akparti_AkRTE", 
"akreperol1955", "AlparslanTurk06", "aozturk70", "asimm4th", 
"ASLAN__5", "AyAzyahu", "bavehayran", "bbulentkayar", "be_yaz_ca", 
"BennKerem", "BerilDeniz77", "bilalardic_", "billpostmus", "bizimmmemleket", 
"BULUT__USTASI", "BurakFBSensoy", "cayelirize", "CicekciKiz_", 
"DobraUzunAdam", "DogrulariYaziyo", "ebaabil", "efendi_insan", 
"Elmanoglu_Drvs", "emre_izmir_", "ErsoyMehmetAkf", "esma_fb_3437", 
"esrefynbsn", "fatihcaglayan25", "favlasanaa", "FenaYazar", "filiz_paker", 
"GabrielAydin", "gonul_insani_", "HanZala32", "holy_sin", "InstagramPlus", 
"IsmetOnc", "KaankutAatay", "kamilekucuker24", "karikatu_r", 
"KemalAta34", "kiliskilis79", "KIRMIZI_BEY4Z_G", "MahirAytekin", 
"MakarnaSos", "MarkGKirshner", "MazlumunFedaisi", "MercanSureyya", 
"MerterSibel", "MoRHoPi", "muhacir1887", "NormanBuffong", "oncevatan81", 
"OrbisTertius3", "osmanlit0runu", "Oyuna_GelmeTR", "ResmiZaytung", 
"rt_liyorum", "RTerdog4n", "RTErdog4n", "Saglam_Iradee", "Sanki_biri", 
"Semih_Kural", "sessiz_ciglik0", "sevdamizzz", "sezerhsn", "siyaset__name", 
"Siyasi_Yazar", "son3er", "Sussam_Olmuyor_", "Telefizyon", "trakyali77", 
"twit_komedyeni", "TwitineGeldim", "uguronal", "ultrasKan1071", 
"yakupaltinoz", "Yazar212", "YEA1453", "Yn1Dunya", "Yobaz_Zeynep", 
"Yobaz4K", "zekibahce"), class = "factor"), degree.in = c(0L, 
0L, 0L, 0L, 3L, 0L, 0L, 35L, 0L, 0L, 12L, 0L, 71L, 0L, 29L, 0L, 
0L, 330L, 0L, 0L, 207L, 0L, 2L, 337L, 0L, 23L, 0L, 113L, 0L, 
112L, 19L, 0L, 0L, 29L, 0L, 0L, 0L, 10L, 13L, 9L, 0L, 0L, 0L, 
0L, 26L, 0L, 185L, 0L, 285L, 0L, 49L, 152L, 0L, 0L, 11L, 96L, 
1324L, 45L, 0L, 0L, 3L, 1110L, 0L, 0L, 38L, 0L, 40L, 0L, 4L, 
7L, 0L, 13L, 103L, 0L, 40L, 65L, 12L, 0L, 0L, 0L, 2L, 0L, 0L, 
0L, 562L, 0L, 0L, 0L, 225L, 0L, 0L, 93L, 0L, 70L, 0L, 0L, 1L, 
0L, 0L, 4L), friends_count = c(549982L, 360141L, 292551L, 264835L, 
192642L, 190477L, 153839L, 127660L, 126163L, 124043L, 115284L, 
113246L, 109422L, 107159L, 105183L, 101006L, 100667L, 99427L, 
98606L, 97909L, 95053L, 94830L, 92558L, 90112L, 89117L, 88388L, 
87969L, 87650L, 86907L, 85422L, 85395L, 84870L, 84747L, 83611L, 
83357L, 82381L, 82227L, 81084L, 80187L, 78807L, 76336L, 76231L, 
73940L, 73840L, 73759L, 73669L, 73324L, 73268L, 72944L, 72806L, 
71729L, 71208L, 70621L, 69982L, 69669L, 69432L, 69391L, 68880L, 
68478L, 67651L, 67514L, 66855L, 66472L, 66396L, 66309L, 65044L, 
64248L, 64038L, 63455L, 63275L, 62500L, 62388L, 61696L, 61212L, 
61154L, 61077L, 60700L, 60487L, 60426L, 60265L, 60264L, 58954L, 
58907L, 58903L, 58671L, 58086L, 57856L, 57793L, 57127L, 56910L, 
56508L, 56506L, 56466L, 56378L, 56343L, 56334L, 56314L, 56169L, 
55864L, 55858L)), .Names = c("screen_name", "degree.in", "friends_count"
), class = "data.frame", row.names = c(NA, -100L))
1
  • 2
    this might not solve your problem, but just a note that will help you when using ggplot2: if you declare that data=the.table[[i]], then when you declare things inside aes, don't re-write the data frame with $ notation; just write the variable name (data=the.table[[i]],aes(x=friends_count) . it will be evaluated in the declared data frame environment. As it stands, you're sending it a vector, rather than simply letting it evaluate the variable correctly. Commented Sep 25, 2015 at 5:40

1 Answer 1

4

The sample data contains several entries with degree.in equal to zero. The logarithm of zero is -Inf, which is sometimes represented as NA. In any case it is not a meaningful value in a plot. I suggest that you make sure that all your data points have positive values if you want to represent them on log scale.

You may try to plot a corresponding subset of your data using, e.g.,

the.table_pos <- the.table[the.table$degree.in > 0,]
Sign up to request clarification or add additional context in comments.

4 Comments

You're welcome; @eabanoz . I'm glad that I could help.
Hi RHertel, I used your suggestion and it gives this error. Error in ifelse(!is.na(scale$limits), scale$limits, scale$range$range) : replacement has length zero In addition: Warning message: In rep(no, length.out = length(ans)) : 'x' is NULL so the result will be NULL .Do you have any idea?
Sorry - I'm not sure I can help. According to your dput() output it seems that your data is stored as a data frame. I thought it was a list. Unfortunately I have to leave now. The warning message was due to occurrences of log(0). Maybe one could just live with this warning (it's not an error after all), or set the values that are zero to NA.
Hi RHertel, it was originally a list object but your code turns it into data frame. May main problem is even thought I get rid off all 0's plot continue to give the same error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.