Set Reference Points for Overlaying Data onto an image in R - image

I have seen a few questions on here (one was even by me) with respect to using R to plot an image.
The difference here is that I need to set the reference cooridnates for my image to match the data I am looking to plot on top of the image.
More specifically, I need R to understand that the coordinates for the background image are x = (-100,100) and y = (40,-40).
I have been able to to read in the image file and plot it using the ReadImages package, but when I overlay my data using points(), the data obviously do not line up appropriately.
Any help is much appreciated.
EDIT: here are some example data and I attached the image:
structure(list(teamid = c("6", "6", "6", "6", "6", "6", "2",
"6", "6", "6", "2", "6", "10", "10", "10", "10", "20", "20",
"10", "10", "10", "20", "20", "20", "10", "10"), xcoord = c("79",
"81", "33", "34", "75", "52", "-67", "80", "44", "79", "-53",
"54", "-55", "-81", "-66", "-66", "45", "81", "-78", "-70", "-59",
"50", "53", "63", "-79", "-78"), ycoord = c("0", "0", "-18",
"-20", "6", "-11", "-7", "7", "-28", "-10", "35", "22", "25",
"-5", "25", "23", "-11", "13", "22", "16", "13", "23", "7", "16",
"8", "8")), .Names = c("teamid", "xcoord", "ycoord"), class = "data.frame", row.names = c(74328L,
74331L, 74332L, 74334L, 74336L, 74338L, 74340L, 74341L, 74346L,
74347L, 74348L, 74349L, 100136L, 100137L, 100138L, 100139L, 100147L,
100148L, 100151L, 100154L, 100156L, 100158L, 100159L, 100161L,
100163L, 100167L))

You can create an empty plot with the correct dimensions, then use the rasterImage function to plot the image, then adding the points should work fine.
Another approach is to use the updateusr function from the TeachingDemos package after plotting the image to make sure that the coordinates match what you want them to before adding lines or points.
I saved your graphic above as ice.png and ran the following code:
library(EBImage)
ice <- readImage('My Pictures/ice.png')
pos <- structure(list(teamid = c("6", "6", "6", "6", "6", "6", "2",
"6", "6", "6", "2", "6", "10", "10", "10", "10", "20", "20",
"10", "10", "10", "20", "20", "20", "10", "10"), xcoord = c("79",
"81", "33", "34", "75", "52", "-67", "80", "44", "79", "-53",
"54", "-55", "-81", "-66", "-66", "45", "81", "-78", "-70", "-59",
"50", "53", "63", "-79", "-78"), ycoord = c("0", "0", "-18",
"-20", "6", "-11", "-7", "7", "-28", "-10", "35", "22", "25",
"-5", "25", "23", "-11", "13", "22", "16", "13", "23", "7", "16",
"8", "8")), .Names = c("teamid", "xcoord", "ycoord"),
class = "data.frame", row.names = c(74328L,
74331L, 74332L, 74334L, 74336L, 74338L, 74340L, 74341L, 74346L,
74347L, 74348L, 74349L, 100136L, 100137L, 100138L, 100139L, 100147L,
100148L, 100151L, 100154L, 100156L, 100158L, 100159L, 100161L,
100163L, 100167L))
pos$xcoord <- as.numeric(pos$xcoord)
pos$ycoord <- as.numeric(pos$ycoord)
ice2 <- as.raster(ice)
pin <- par('pin')
plot( c(-100,100), c(-40,40), type='n', xlab='', ylab='',
asp=pin[1]/pin[2], axes=FALSE, xaxs='i', yaxs='i')
rasterImage(ice2, -100, -40, 100, 40, interpolate=FALSE)
with(pos, text(xcoord, ycoord, teamid, col='green', cex=1.2) )
Does this do what you want?

Related

How to make a faceted cell graph in ObservableHQ?

I have the following array of objects:
[{ "userid": 1, "groupid": 66123, "date_joined": "2023-01-01", "d0": "10", "d1": "6", "d2": "0", "d3": "1", "d4": "0", "d5": "0", "d6": "0", "d7": "0", "d8": "0", "d9": "0", "d10": "0", "d11": "0", "d12": "0", "d13": "0", "d14": "0" },
{ "userid": 2, "groupid": 66123, "date_joined": "2023-01-01", "d0": "0", "d1": "0", "d2": "2", "d3": "0", "d4": "0", "d5": "0", "d6": "0", "d7": "0", "d8": "0", "d9": "0", "d10": "0", "d11": "0", "d12": "0", "d13": "0", "d14": "0" },
{ "userid": 3, "groupid": 68100, "date_joined": "2023-01-01", "d0": "0", "d1": "0", "d2": "0", "d3": "0", "d4": "0", "d5": "0", "d6": "0", "d7": "0", "d8": "0", "d9": "0", "d10": "0", "d11": "0", "d12": "0", "d13": "0", "d14": "0" }]
and I want to create a faceted cell graph like the following:
where:
each facet is determined by the "groupid"
each row in the facet is the "userid"
there are 15 blocks on each row, corresponding to the respective "d0" - "d14" columns.
Example:
The first facet would be for groupid "66123", with two rows: userid "1" and userid "2". Each row would have 15 columns, each one corresponding to the respective "d0" - "d14" columns.
This code is from the example notebook:
Plot.plot({
height: 1400,
x: {
axis: null,
padding: 0,
},
y: {
padding: 0,
tickFormat: Plot.formatWeekday("en", "narrow"),
tickSize: 0
},
fy: {
reverse: true
},
facet: {
data: DJI,
y: d => d.Date.getUTCFullYear()
},
color: {
type: "diverging",
scheme: "PiYG"
},
marks: [
Plot.cell(DJI, {
x: d => d3.utcWeek.count(d3.utcYear(d.Date), d.Date),
y: d => d.Date.getUTCDay(),
fill: (d, i) => i > 0 ? (d.Close - DJI[i - 1].Close) / DJI[i - 1].Close : NaN,
title: (d, i) => i > 0 ? ((d.Close - DJI[i - 1].Close) / DJI[i - 1].Close * 100).toFixed(1) : NaN,
inset: 0.5
})
]
})
I'm not sure how to change the x: d => d3.utcWeek.count(d3.utcYear(d.Date), d.Date), with to get the graph that I need.
Any help would be greatly appreciated!
Can I suggest that you share a notebook with what you have tried so far? It will be easier to help you if we see where you're at.

Laravel add variable for nested collections

I want to add variable to each object of collection inside collection. Here JSON response like this:
{
"id": 16,
"survey_id": "8",
"title": "How are you?",
"created_at": "2020-02-06 04:21:44",
"updated_at": "2020-02-06 04:21:44",
"answers": [
Here I want to add variable to each answer
{
"id": 52,
"question_id": "16",
"text": "VERY GOOD",
"created_at": "2020-02-06 04:21:44",
"updated_at": "2020-02-06 04:21:44",
"reports_count": "4",
"responded": 2
},
{
"id": 53,
"question_id": "16",
"text": "OK",
"created_at": "2020-02-06 04:21:44",
"updated_at": "2020-02-06 04:21:44",
"reports_count": "4",
"responded": 2
},
{
"id": 54,
"question_id": "16",
"text": "BAD",
"created_at": "2020-02-06 04:21:44",
"updated_at": "2020-02-06 04:21:44",
"reports_count": "2",
"responded": 2
}
]
},
Overall, I want to add for every answers variable like $respond = $answer->reports()->count(); Help PLease!!
You could do something like this, if you want to calculate it in the collection directly.
$yourCollection->transform(function($collectionItem) {
$collectionItem['responded'] = $collectionItem->answers->count();
return $collectionItem;
});
But you could do this also on the sql side. I think it should be also the better way ;)
I would fetch the answers from the database before assigning that array to a new question-object-property.
public function get() {
// fetch parent question
$question = Question::where('title', $title)->get();
// fetch answers
$answers = Answers::where('parent_id', $question->id)->get();
// create property called "answers"
$question->answers = $answers;
return response()->json($question);
}

swift get currencycode from country code

I find the accepted answer to get currencycode from country code in objective c here - How to get ISOCurrencyCode from a ISOCountryCode in iphone sdk?
Can someone help with the swift code to do the same ?
let components: [String: String] = [NSLocaleCountryCode: "CA"]
let identifier = NSLocale.localeIdentifierFromComponents(components)
let locale = NSLocale(localeIdentifier: identifier)
let currencyCode = locale.objectForKey(NSLocaleCurrencyCode)
print(currencyCode)
public static func convertStringCurrencyToNumber(strCurrency:String, locale:String)->Double {
var formatter = NSNumberFormatter()
formatter.currencyCode = locale
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
if let converted:Double = formatter.numberFromString(strCurrency)?.doubleValue {
return converted
}else{
return 0.0
}
}
Changing formatter.locale to formatter.currencyCode works as expected with the ISO 4127 Country and Currency code "USD".
Use below this return type function to get country_code:-
//MARK:- GET COUNTRY CODE
func getCountryPhonceCode (_ country : String) -> String
{
let x : [String] = ["972", "IL",
"93" , "AF",
"355", "AL",
"213", "DZ",
"1" , "AS",
"376", "AD",
"244", "AO",
"1" , "AI",
"1" , "AG",
"54" , "AR",
"374", "AM",
"297", "AW",
"61" , "AU",
"43" , "AT",
"994", "AZ",
"1" , "BS",
"973", "BH",
"880", "BD",
"1" , "BB",
"375", "BY",
"32" , "BE",
"501", "BZ",
"229", "BJ",
"1" , "BM",
"975", "BT",
"387", "BA",
"267", "BW",
"55" , "BR",
"246", "IO",
"359", "BG",
"226", "BF",
"257", "BI",
"855", "KH",
"237", "CM",
"1" , "CA",
"238", "CV",
"345", "KY",
"236", "CF",
"235", "TD",
"56", "CL",
"86", "CN",
"61", "CX",
"57", "CO",
"269", "KM",
"242", "CG",
"682", "CK",
"506", "CR",
"385", "HR",
"53" , "CU" ,
"537", "CY",
"420", "CZ",
"45" , "DK" ,
"253", "DJ",
"1" , "DM",
"1" , "DO",
"593", "EC",
"20" , "EG" ,
"503", "SV",
"240", "GQ",
"291", "ER",
"372", "EE",
"251", "ET",
"298", "FO",
"679", "FJ",
"358", "FI",
"33" , "FR",
"594", "GF",
"689", "PF",
"241", "GA",
"220", "GM",
"995", "GE",
"49" , "DE",
"233", "GH",
"350", "GI",
"30" , "GR",
"299", "GL",
"1" , "GD",
"590", "GP",
"1" , "GU",
"502", "GT",
"224", "GN",
"245", "GW",
"595", "GY",
"509", "HT",
"504", "HN",
"36" , "HU",
"354", "IS",
"91" , "IN",
"62" , "ID",
"964", "IQ",
"353", "IE",
"972", "IL",
"39" , "IT",
"1" , "JM",
"81", "JP", "962", "JO", "77", "KZ",
"254", "KE", "686", "KI", "965", "KW", "996", "KG",
"371", "LV", "961", "LB", "266", "LS", "231", "LR",
"423", "LI", "370", "LT", "352", "LU", "261", "MG",
"265", "MW", "60", "MY", "960", "MV", "223", "ML",
"356", "MT", "692", "MH", "596", "MQ", "222", "MR",
"230", "MU", "262", "YT", "52","MX", "377", "MC",
"976", "MN", "382", "ME", "1", "MS", "212", "MA",
"95", "MM", "264", "NA", "674", "NR", "977", "NP",
"31", "NL", "599", "AN", "687", "NC", "64", "NZ",
"505", "NI", "227", "NE", "234", "NG", "683", "NU",
"672", "NF", "1", "MP", "47", "NO", "968", "OM",
"92", "PK", "680", "PW", "507", "PA", "675", "PG",
"595", "PY", "51", "PE", "63", "PH", "48", "PL",
"351", "PT", "1", "PR", "974", "QA", "40", "RO",
"250", "RW", "685", "WS", "378", "SM", "966", "SA",
"221", "SN", "381", "RS", "248", "SC", "232", "SL",
"65", "SG", "421", "SK", "386", "SI", "677", "SB",
"27", "ZA", "500", "GS", "34", "ES", "94", "LK",
"249", "SD", "597", "SR", "268", "SZ", "46", "SE",
"41", "CH", "992", "TJ", "66", "TH", "228", "TG",
"690", "TK", "676", "TO", "1", "TT", "216", "TN",
"90", "TR", "993", "TM", "1", "TC", "688", "TV",
"256", "UG", "380", "UA", "971", "AE", "44", "GB",
"1", "US", "598", "UY", "998", "UZ", "678", "VU",
"681", "WF", "967", "YE", "260", "ZM", "263", "ZW",
"591", "BO", "673", "BN", "61", "CC", "243", "CD",
"225", "CI", "500", "FK", "44", "GG", "379", "VA",
"852", "HK", "98", "IR", "44", "IM", "44", "JE",
"850", "KP", "82", "KR", "856", "LA", "218", "LY",
"853", "MO", "389", "MK", "691", "FM", "373", "MD",
"258", "MZ", "970", "PS", "872", "PN", "262", "RE",
"7", "RU", "590", "BL", "290", "SH", "1", "KN",
"1", "LC", "590", "MF", "508", "PM", "1", "VC",
"239", "ST", "252", "SO", "47", "SJ",
"963","SY",
"886",
"TW", "255",
"TZ", "670",
"TL","58",
"VE","84",
"VN",
"284", "VG",
"340", "VI",
"678","VU",
"681","WF",
"685","WS",
"967","YE",
"262","YT",
"27","ZA",
"260","ZM",
"263","ZW"]
var keys = [String]()
var values = [String]()
let whitespace = CharacterSet.decimalDigits
//let range = phrase.rangeOfCharacterFromSet(whitespace)
for i in x {
// range will be nil if no whitespace is found
if (i.rangeOfCharacter(from: whitespace) != nil) {
values.append(i)
}
else {
keys.append(i)
}
}
let countryCodeListDict = NSDictionary(objects: values as [String], forKeys: keys as [String] as [NSCopying])
if let t: AnyObject = (countryCodeListDict as? [String : AnyObject])? [country] {
return countryCodeListDict[country] as! String
} else
{
return ""
}
}

foreach item in an array find largest record using mongodbcsharp driver

Suppose there is an document like:
{
"_id": "1cf2080c-ce9a-367b-93ba-dbf7b2bf8a2c",
"template_id" : "1cf2080c-ce9a-367b-93ba-dbf7b2bf8a2c",
"some_value" : 12
}
mapping to a class as Template, and given a templateid array array_id = ["aa", "bb", "cc"]
question: how can I get a Template List using mongodb C# driver2.1 up, which each item in list have largest some_value ?
for example there is a collection:
{"_id": "1", "template_id": "aa", "some_value":1}
{"_id": "2", "template_id": "aa", "some_value":2}
{"_id": "3", "template_id": "aa", "some_value":3}
{"_id": "4", "template_id": "bb", "some_value":4}
{"_id": "5", "template_id": "bb", "some_value":5}
and a template_id_array = ["aa", "bb"]
expect result is:
{"_id": "3", "template_id": "aa", "some_value":3}
{"_id": "5", "template_id": "bb", "some_value":5}
I think it would be some "aggregate" or "map-reduce" like procedure, first match result and then group by template id and then find max somevalue for each group.
I have not tested this but should work with some probable modifications
var result = new List<Template>();
foreach (var element in template_id_array)
{
var filter = builder.Eq("TemplateId", element);
var doc = await collection.Find(filter)
.SortByDescending(x => x.SomeValue)
.Limit(1)
.FirstOrDefaultAsync();
result.Add(doc);
}
Then you can serialize it to the final form:
var expectedResult = JsonConvert.SerializeObject(result);

kendo data source group

We are trying to basically and literally group data in a data source like you can do with SQL when selecting against a table.
So I get data that looks like the following:
var grid = $("#grid").kendoGrid({
dataSource: {
data: [
{f1: "1", f2:"2", f3:"3", f4:"4", f5:"5", f6: "1"},
{f1: "1", f2:"2", f3:"3", f4:"4", f5:"5", f6: "3"},
{f1: "1", f2:"2", f3:"3", f4:"6", f5:"7", f6: "2"},
{f1: "1", f2:"2", f3:"3", f4:"6", f5:"7", f6: "1"},
{f1: "1", f2:"2", f3:"3", f4:"6", f5:"5", f6: "2"}
]},
}).data("kendoGrid");
We want to group by f1, f2, f3, f4 and sum on f6.
The result when we show this data in a grid or list view should be that the data source has only 2 rows in it
{f1: "1", f2:"2", f3:"3", f4:"4", sum: "4"},
{f1: "1", f2:"2", f3:"3", f4:"6", sum: "5"},
How can I accomplish this?

Resources