Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to can plot with R pdf device while labels are arabic or persian fonts?

Tags:

r

pdf

I have a database of a country villages which the name of villages are recorded with Arabic fonts (such as "احمد آباد"). I would like to plot some variables vs. the name of villages with pdf device of R., but after plotting, all Villages names were shown with dot(........) in plot. I also tried to import database file with UTF-8 encoding but this also not resolved the problem only with a simple difference, in this case unknown characters(..†..§....†ˆ....„§...ƒ.±) were replaced with dots. I also used extrafont library to load fonts to pdf device and then embed them to file but it also not resolved the problem. please help me, I searched within posts but I dont find my answer.

when i run head commend the names of villages are unknown characters as follows. I think before plotting this issue should be resolved.

> head(AllData)
  Row  City   District              Village Sampl.P. Day Month Year Tur.
2   1 Baneh   ط¢ظ„ظˆطھ         ط¨ط§ط±ط¢ظˆط±  Network   4     4   88 0.14
3   2 Baneh   ط¢ظ„ظˆطھ         ط¨ط§ط±ط¢ظˆط±  Network   4     4   88 0.12
4   3 Baneh ظ†ظ…ط´ظٹط±           ظƒظˆط®ط§ظ†  Network  21    10   88 3.36
5   1 Bijar ظƒط±ط§ظ†ظٹ    ط´ظٹط® ط¨ط´ط§ط±طھ  Network  25     1   88 0.27
6   2 Bijar ظƒط±ط§ظ†ظٹ         ط¢ظ„ظ¾ظ‡ظˆطھ  Network  11     2   88 0.14
7   3 Bijar ظƒط±ط§ظ†ظٹ ظ‚ط±ظ‡ ظ…ط­ظ…ط¯ ظ„ظˆ  Network   3     3   88 0.74

the mean values of my variable versus the names of villages are potted in the following figure. I use the following function for making the plot:

MeanRef1<- function(x,y ,DATA,v1,XLAB,YLAB,v2 ,XLIM ){
  group.CI(x~y ,DATA) 
  with(group.CI(x~y,DATA),
      segplot(y~x.lower+x.upper,center=x.mean,
      draw.bands = FALSE,  segments.fun = panel.arrows, 
      ends = "both", angle = 90, length = .03, 
      par.settings = simpleTheme(pch = 19, col = 1,cex = 0.5), 
      xlab = paste(XLAB,"±","SE"),
      ylab=YLAB,
      xlim=XLIM,
        panel = function(x, y, z, ...) { 
        panel.abline(h = z, col = "grey", lty = "dashed") 
        panel.abline(v = v1, col = "red") 
        panel.abline(v = v2, col = "red")   
        panel.segplot(x, y, z, ...)})  ) 

}

and the following is the code for calling the function:

pdf("CCPPVillageGnd.pdf", width=5, height=16,  pointsize = 600)# Start graph---
MeanRef1(AllData[which(AllData$Sampl.P.=="Groundwater "), ]$CCPP,AllData[which(AllData$Sampl.P.=="Groundwater "), ]$Village,AllData,4,"CCPP", "Village",10,c(-10,20) )
dev.off()# END graph------ 

the mean values of my variable versus the names of villages

following is the dput() of head.

> dput(head(AllData))
structure(list(Row = c(1L, 2L, 3L, 1L, 2L, 3L), City = structure(c(1L, 
1L, 1L, 2L, 2L, 2L), .Label = c("Baneh", "Bijar", "Dehgolan", 
"Divandareh", "Kamyaran", "Marivan", "Qorveh", "Saghez", "Sanandaj", 
"Sarvabad"), class = "factor"), District = structure(c(3L, 3L, 
21L, 16L, 16L, 16L), .Label = c("", "اورامان", "آلوت", "بوالحسن", 
"پشت آربابا", "چنگ الماس", "چهاردولي", "خاوميرآباد", "خاووميرآباد", 
"زيويه", "سارال", "سرشيو", "سريش آباد", "شوي", "كاني سور", "كراني", 
"كرفتو", "كلاترزان", "مركزي", "موچش", "نمشير", "ننور", "ئيلاق"
), class = "factor"), Village = structure(c(38L, 38L, 323L, 243L, 
30L, 270L), .Label = c("", "ابراهيم آباد", "ابراهيم آباد ياپل", 
"اجگره", "احمد كر", "احمدآباد", "اسلام آباد", "اسلامدشت", "اشرف آباد", 
"اشرف آباد قوچ", "اكبرآباد", "امين آباد", "امين آباد قصلان", 
"انجيران", "انگوژان", "انگيجه", "انگينه", "اورامان", "آبباريك", 
"آبفار", "آبيدر", "آخكند", "آرندان", "آرين", "آزادويس عليا", 
"آغبلاغ ", "آق تپه", "آقبلاق", "آل كبود", "آلپهوت", "آلي همدان", 
"آونگان", "آهنگران", "بابارشاني", "باباريز", "باخرم", "باخله", 
"بارآور", "باشبلاغ", "باشقشلاق", "باشماق", "باغان", "باقرآباد", 
"بان سعيد", "باينچوب", "بدر", "برچم", "برده رش", "برده رشه خاوميرآباد", 
"برده سفيد", "برقرو", "برقلعه", "برويشكاني", "بلبان آباد", "بلدستي", 
"بلكه", "بوالحسن", "بوانه", "بوريدر", "بهارستان", "بهارلو", "بهرام گنبد", 
"بيدرشان", "بيساران", "بيلو", "بيلوار", "بيله", "پاپاله", "پايگلان", 
"پشت آربابا", "پشته", "پلوره", "پيرخضران", "پيرصفا", "پيريونس", 
"پيله", "تاته رشيد", "تازه آباد صوفي بله", "تازه آباد عيسي آباد", 
"تازه آباد قروچاي", "تبريز خاتون", "تخت زنگي", "ترخان آباد", 
"تفين", "تنگي سر", "توپ آغاج", "تودارصمدي", "توداروته", "تيژتيژ", 
"جانقلي", "جبرئيلان", "جشني آباد", "جعفرآباد", "جعفرآباد سياه منصور", 
"جيرانمينگه", "چارلي بلاغ", "چالاب", "چاولكان وزير", "چپقلو", 
"چراغ آباد", "چرميله", "چشمه كاظم", "چشمه منتش", "چطاق", "چمقلو", 
"چناره", "چناره ", "چوپي", "چور", "چور و ننه", "چولجه", "چومان", 
"چوئين", "چهاردولي غربي", "چهارگاه", "چي چي خوار", "حاج عبدل", 
"حاجي آباد", "حاجي پمق", "حسن آباد", "حسن آباد كنگره", "حسن آباد گرگان", 
"حسن آبله", "حسن قره", "حسين آباد", "حسين آباد شمالي", "حسين آباد گرگان", 
"حقه", "حيدرديده بان", "خSarvabad", "خامسان", "خانباغي", "خانقاه حسن جادار", 
"خانم كن", "خروسه", "خريله", "خشكدره", "خضران", "خليچيان", "خليفه ترخان", 
"خواجه مير", "خوري آباد", "خوشمقام", "دادانه كمانگر", "دانيكش", 
"درگاه شيخان", "درويان", "درويان عليا", "درويش خاكي", "دره اسب", 
"دره تفي", "دره نخي", "دره هرد", "دري", "دزلي", "دل", "دلبران", 
"دميو", "دورود", "دوزخدره", "دوسر", "دويسه", "ده بنياد", "ده رقه پيرحسين", 
"ده كانان", "ديوزند", "ذكي بيگ عليا", "رزاب", "رستمان", "رشه ده", 
"رشيد قلعه", "رشيدآباد", "رودبار", "ريخلان", "زاغه عليا", "زرده كمر", 
"زرواو", "زرواو سفلي", "زرينجوب", "زرينه", "زريوار", "زلي", "زويران", 
"زيوه", "زيويه", "ژان", "ژاورود", "ژريژه", "ژنين", "سارال", "ساوجي", 
"سرا", "سراب", "سراب قاميش", "سرابكام", "سرچشمه", "سرخه توت", 
"سرخه جوب", "سردوش", "سرشيو", "سرقلعه", "سركل", "سرينجيانه", 
"سعدآباد", "سعيدآباد", "سلسي عليا", "سلطان آباد دره ويران", "سليمان كندي", 
"سنته", "سنگ سفيد", "سنگين آباد", "سورال", "سورسور", "سورين", 
"سياسران", "سياسران سفلي", "سياناو", "سيس", "سيف آباد", "سيف سفلي", 
"سيف عليا", "سيلتان", "سيويچ سفلي", "سيويچ عليا", "شاراني", "شاليار", 
"شانوره", "شاه نشين", "شاه ولي", "شاهجوب", "شاهو", "شاهيني", 
"شجاع آباد", "شركه", "شريف كندي", "شعباني", "شوي", "شهابيه", 
"شهراني", "شهرك صنعتي", "شهرك كراني", "شهرك ورمهنگ", "شيخ بشارت", 
"شيخ تقه", "شيخ عطار", "شيخه كويره", "شيرين بلاغ", "صادق آباد", 
"صادق آباد قادرمرز", "صوفي بله", "طينال", "ظله جوب", "عباس آباد", 
"عسكران", "علي آباد", "علي آباد جنت بو", "علي آباد كرفتو", "علي آباد ياپل", 
"فرج آباد", "فقيه كان", "قادرآباد", "قادرمرز", "قاسم آباد", "قاملو", 
"قباسرخ", "قتلو", "قراتوره", "قرخلر", "قروچاي", "قره محمد لو", 
"قزلبلاغ", "قزلتپه", "قطوند", "قلعه", "قلعه جقه عليا", "قلعه جي", 
"قلعه روتله", "قلعه ريحانه", "قلعه شيخان", "قلعه فولاد", "قلعه كهنه پاپاله ", 
"قلعه گاه عليا", "قلعه گاه گودرز", "قلقله", "قوچاق", "قوخ", "قورت دره", 
"قوري چاي", "قول استر", "قهرآباد حومه", "قينرجه", "كاظم آباد", 
"كاقلي", "كاكوذكريا", "كامشگران", "كاني پهن", "كاني چاي", "كاني حسين بگ", 
"كاني دينار", "كاني سانان", "كاني سفيد احمد كر", "كاني سفيد كلكان", 
"كاني سور", "كاني شاقلي", "كاني شيرين", "كاني گويز", "كاني ميران", 
"كبودخاني عليا", "كتك", "كچه گنبد پيرتاج", "كرگ آباد", "كرگينه", 
"كروندان", "كژيكران", "كلاترزان", "كلاتي", "كلجي", "كمال آباد شهابيه", 
"كوپه قران", "كوتان سفلي", "كوچر", "كوخ مامو", "كوخان", "كوره دره سفلي", 
"كوره دره عليا", "كوسالان", "كول", "كولان", "كوله ساره", "كوليت", 
"كوماسي", "كومائين", "كيله كبود", "گازرخاني", "گاگل", "گاورود", 
"گاوشله", "گاومله", "گرگرعليا", "گرگه اي", "گزان سفلي", "گل تپه", 
"گل تپه سفلي", "گل تپه عليا", "گلان", "گلانه", "گلتپه", "گلچيدر", 
"گلستانه", "گله", "گلي كران", "گليه", "گماره لنگ", "گنداب عليا", 
"گندمان", "گوشخاني", "گوگتپه", "گومه اي", "گويله", "گيلي كران", 
"لاويتان", "لاين", "لره دول", "لك", "لنج آباد", "لنگريز", "لون سادات", 
"لون كهنه", "م . برده رشه خاوميرآباد", "م . بيلو", "م. دزج", 
"م.آغيازي", "ماسان", "ماسيدر", "ماقوت", "ماموله", "ماويان", "مبارك آباد", 
"مجتمع تحقيقات كشاورزي", "مجتمع دزج", "مجتمع ريخلان", "مجين", 
"محراب", "محمد آباد كريان", "محمد علي آباد", "محمدعلي آباد", 
"محمده", "مرادآباد", "مرگ", "مرواريد", "مشيرآباد اوريه", "مظفرآباد", 
"موسك", "موكه", "ميدول", "ميسوراب", "ميك", "ميهم سفلي", "ميهم عليا", 
"ناوه", "نبي آباد", "نجف آباد", "نجنه رضا", "نران", "نزار", "نساره ", 
"نساره سفلي", "نساره عليا", "نسل", "نگارستان", "نگل", "نمشير", 
"ننور", "نوبهار", "نورمحمدكندي", "نه نه", "ني", "ني آباد", "وزير", 
"وله ژير", "ولي آباد", "وندرني سفلي", "وندرني عليا", "ويسه", 
"ويله", "وينسار", "هانه شيخان", "هانيس", "هجرت", "هرسين", "هزاركانيان", 
"هشتادجفت", "هلتوشان", "هلو", "هليز آباد", "هنگه چينه", "هنگه ژاله", 
"يازيبلاغي", "يعقوب آباد", "يلغوزآغاج", "يمينان سفلي", "ينگجه", 
"ينگي كند سياه منصور", "ئيلان جنوبي", "ئيلان شمالي"), class = "factor"), 
    Sampl.P. = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Groundwater ", 
    "Network", "Reservoir"), class = "factor"), Day = c(4, 4, 
    21, 25, 11, 3), Month = c(4, 4, 10, 1, 2, 3), Year = c(88, 
    88, 88, 88, 88, 88), Tur. = c(0.14, 0.12, 3.36, 0.27, 0.14, 
    0.74), T = c(28.1, 27.6, 19.5, 21.2, 23.3, 25.5), pH = c(6.7, 
    6.6, 7.56, 7.4, 7.1, 7.7), Ecmh = c(394.2, 277.4, 408.8, 
    876, 313.9, 1905.3), Ecms = c(39.42, 27.74, 40.88, 87.6, 
    31.39, 190.53), TDS = c(252.3, 174.8, 261.6, 569.4, 200.9, 
    1276.6), TH = c(186.8, 145.3, 305.2, 380.5, 186.3, 704.6), 
    Cl = c(18.8, 6.9, 13.9, 28.9, 6, 101.2), SO4 = c(10, 2, 64, 
    156, 10, 600), CO3 = c(0, 0, 0, 0, 0, 0), Alk = c(119.1, 
    138.2, 149, 314.9, 144.2, 132.1), NO3 = c(32, 7.5, 10, 10, 
    16.3, 60), NO2 = c(0, 0, 0, 0, 0, 0), Ca = c(68.1, 43.2, 
    70.4, 104.8, 35.7, 86.4), Mg = c(4, 9.1, 31.4, 28.8, 23.6, 
    118.7), Na = c(NA, NA, 12, NA, NA, NA), K = c(NA, NA, 0, 
    NA, NA, NA), Mn = c(0, 0, 0, 0, 0, 0), As = c(0, 0, 0, 0, 
    0, 0), CO2 = c(4, 7, 20, 14, 8, 4), LI = c(-0.815683775, 
    -1.031083193, -0.035458255, 0.262299098, -0.697756373, 0.092070534
    ), RY = c(8.33136755, 8.662166387, 7.630916509, 6.875401803, 
    8.495512746, 7.515858932), CCPP = c(-50.9138138, -78.58783739, 
    0.045250252, 26.84998173, -28.20836319, 3.488737546), LS = c(0.310504536, 
    0.085594517, 0.581772243, 0.648044359, 0.131196584, 5.869947015
    ), SL = c(0.051036087, 0.033989865, 0.295660938, 0.45829351, 
    0.069971995, 0.223251159), AI = c(11.00613116, 10.77306408, 
    11.97783121, 12.31560623, 11.20870576, 12.15448885), PSI = c(7.81933249, 
    8.094229466, 7.631902546, 7.105043264, 8.026274918, 7.660141106
    ), DFI = c(0.083049363, 0.049943642, 0.501396512, 1.031986052, 
    0.108162079, 0.725720681), ME = c(-4.14803e-06, -6.43682e-06, 
    1.76907e-08, 5.74793e-06, -7.98249e-06, 3.79148e-06), H2Co3 = c(0.000899942, 
    0.001355815, 0.000170536, 0.000473944, 0.000463594, 8.55779e-05
    ), Hco3 = c(0.002378325, 0.002760271, 0.002964313, 0.006269787, 
    0.002876898, 0.002614427), Co3 = c(8.28637e-07, 7.19049e-07, 
    6.37456e-06, 1.11734e-05, 2.22873e-06, 1.22379e-05), CT = c(0.003279095, 
    0.004116806, 0.003141224, 0.006754904, 0.003342721, 0.002712242
    ), pHs = c(7.515683775, 7.631083193, 7.595458255, 7.137700902, 
    7.797756373, 7.607929466), pHeq = c(7.21203506, 7.167936921, 
    7.559013963, 7.170358539, 7.569237828, 7.555717826)), .Names = c("Row", 
"City", "District", "Village", "Sampl.P.", "Day", "Month", "Year", 
"Tur.", "T", "pH", "Ecmh", "Ecms", "TDS", "TH", "Cl", "SO4", 
"CO3", "Alk", "NO3", "NO2", "Ca", "Mg", "Na", "K", "Mn", "As", 
"CO2", "LI", "RY", "CCPP", "LS", "SL", "AI", "PSI", "DFI", "ME", 
"H2Co3", "Hco3", "Co3", "CT", "pHs", "pHeq"), row.names = c(NA, 
6L), class = "data.frame")

Using cairo_pdf the following plot produced, but still characters are separated and reversed.

jpeg("CCPPVillageNet.jpg", width=5, height=10, units="in", res=600)
MeanRef1(AllData[which(AllData$Sampl.P.=="Groundwater "), ][1:100,]$CCPP,AllData[which(AllData$Sampl.P.=="Groundwater "), ][1:100,]$Village,AllData,4,"CCPP", "Village",10,c(-10,20) )
dev.off()

enter image description here

the sessionInfo() is also as follows:

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=Farsi_Iran.1256  LC_CTYPE=Farsi_Iran.1256   
[3] LC_MONETARY=Farsi_Iran.1256 LC_NUMERIC=C               
[5] LC_TIME=Farsi_Iran.1256    

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] Cairo_1.5-6         latticeExtra_0.6-26
[3] RColorBrewer_1.1-2  Rmisc_1.5          
[5] plyr_1.8.1          lattice_0.20-29    

loaded via a namespace (and not attached):
[1] grid_3.1.1  Rcpp_0.11.2 tools_3.1.1
> 
like image 942
Mahdi Hadi Avatar asked Jan 26 '15 08:01

Mahdi Hadi


1 Answers

The Cairo package might help here.

library(Cairo)

CairoPDF("test.pdf")
plot(0, main="حمد آباد")
dev.off()

EDIT

Based on your data, the names render ok using cairo_pdf

# add libraries
library(latticeExtra)
library(Rmisc)

# tweak your code so the subset of data plots
cairo_pdf("CCPPVillageGnd.pdf")
MeanRef1(AllData$CCPP, AllData$Village,
         AllData, 4, "CCPP", "Village", 10, c(-10,20) )
dev.off()

which returns

enter image description here

like image 56
user20650 Avatar answered Nov 11 '22 04:11

user20650