Set color of point depending on a value
I have a dataframe that looks like this:
x1= c("Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6")
x2= c(58.73, 57.20, 41.90, 38.00, 47.10, 67.30)
x3= c(16.55, 2.10, 8.80, 23.70, 24.50, 14.40)
x4= c(342, 1900, 283, 832, 212, 1533)
x5= c("rual", "rual", "urban", "suburban", "rual", "urban")
testframe = data.frame(Station=x1, LAT=x2, LON=x3, ALT=x4, AREA=x5)
I want to display the points in 3 different colors. Green for rual, yellow for suburban, red for urban.
But until now I only managed to display them all in one color. I didn this:
library(ggmap)
library(ggplot2)
Europe = get_map(location = "Europe", zoom = 4)
p = ggmap(Europe)
p = p + geom_point(data=testframe, aes(x=testframe$LON, y=testframe$LAT), color = "red", size=1)
p
Can someone help me out please?
1 answer

You could try the following:
p + geom_point(data = testframe, aes(LON, LAT, color = AREA), size = 10) + scale_color_manual(name = "AREA", values = c("rual" = "darkgreen", "suburban" = "yellow", "urban" = "red"))
Or copy/paste this chunk of code:
library(ggmap) library(ggplot2) x1 = c("Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6") x2 = c(58.73, 57.20, 41.90, 38.00, 47.10, 67.30) x3 = c(16.55, 2.10, 8.80, 23.70, 24.50, 14.40) x4 = c(342, 1900, 283, 832, 212, 1533) x5 = c("rual", "rual", "urban", "suburban", "rual", "urban") testframe = data.frame(Station = x1, LAT = x2, LON = x3, ALT = x4, AREA = x5) Europe = get_map(location = "Europe", zoom = 4) p = ggmap(Europe) p + geom_point(data = testframe, aes(LON, LAT, color = AREA), size = 10) + scale_color_manual(name = "AREA", values = c("rual" = "darkgreen", "suburban" = "yellow", "urban" = "red"))
See also questions close to this topic

Extracting Reviewer's Rating in Yelp using rvest
I am trying to use the rvest package (together with using CSS selector) to extract reviews and ratings in R.
My code works in scraping the reviews. However, I am having issues with the ratings since it is stored as an image rather than as a text or number. Is there a workaround in R that would allow me to view the ratings as integer?
Below is my code:
library(rvest) library(dplyr) url<"https://www.yelp.com/biz/savordemoiaustin3" reviews < url %>% read_html() %>% html_nodes(".reviewcontent p")%>% html_text() rating<url %>% read_html() %>% html_nodes(".ratinglarge")%>% as.integer() data.frame(reviews, rating, stringsAsFactors = FALSE) %>% View()
Thanks a lot in advance.

Why does the data.table library force origin to be supplied?
I have a list of durations in seconds that I wish to convert to days, hours, minutes, seconds format in r, and display in Rmarkdown. In Rmarkdown, the following code works:
```{r } library(lubridate) test < structure(list(duration = c(583L, 10657L, 451L, 1794L, 1474L, 1386L)), .Names = "duration", row.names = c(NA, 6L), class = "data.frame") test$seconds < test$duration td < seconds_to_period(test$duration) td < sprintf('%02d %02d:%02d:%02d', day(td), td@hour, minute(td), second(td)) td ```
However, whenever the library
data.table
is added before the code is run, the following error is thrown:Error in as.POSIXlt.numeric(x) : 'origin' must be supplied Calls: <Anonymous> ... sprintf > minute > as.POSIXlt > as.POSIXlt.numeric Execution halted
For example:
```{r } library(lubridate) library(data.table) test < structure(list(duration = c(583L, 10657L, 451L, 1794L, 1474L, 1386L)), .Names = "duration", row.names = c(NA, 6L), class = "data.frame") test$seconds < test$duration td < seconds_to_period(test$duration) td < sprintf('%02d %02d:%02d:%02d', day(td), td@hour, minute(td), second(td)) td ```
Any idea why this happens and how it can be avoided?
I need to access the library
data.table
after converting the duration to this format in order to display the data . 
R: Error in if (any((x  xl) <= 0)) stop("ftrf requires x > xl") : missing value where TRUE/FALSE needed
I am using the function
plkhci
from libraryBhat
to construct Profilelikelihood based confidence and I got an error:Error in if (any((x  xl) <= 0)) stop("ftrf requires x > xl") : missing value where TRUE/FALSE needed
when I run this line:
plkhci(x,nlogf,"beta0")
All my
x
in the list ofx
are all greater thanxl
x < list(label=c("beta0","beta1","gamma"),est=c(est$estimate[1],est$estimate[2],est$estimate[3]),low=c(8.1,0.29,0.09),upp=c(7.6,0.31,0.11))
Following is my complete coding:
#data set.seed(5) library(Bhat) beta0<8 beta1<0.03 gamma<0.0105 alpha<0.05 n<100 u<runif(n) u x<rnorm(n) x c<rexp(100,1/1515) c t1<(1/gamma)*log(1((gamma/exp(beta0+beta1*x))*log(1u))) t1 t<pmin(t1,c) t delta<1*(t1>c) delta length(delta) cp<length(delta[delta==1])/n cp delta[delta==1]<ifelse(rbinom(length(delta[delta==1]),1,0.5),1,2) delta deltae<ifelse(delta==0, 1,0) deltar<ifelse(delta==1, 1,0) deltai<ifelse(delta==2, 1,0) dat=data.frame(t,delta, deltae,deltar,deltai,x) dat$interval[delta==2] < as.character(cut(dat$t[delta==2], breaks=seq(0, 600, 100))) labs < cut(dat$t[delta==2], breaks=seq(0, 600, 100)) dat$lower[delta==2]<as.numeric( sub("\\((.+),.*", "\\1", labs) ) dat$upper[delta==2]<as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) ) data0<dat[which(dat$delta==0),]#uncensored data data1<dat[which(dat$delta==1),]#right censored data data2<dat[which(dat$delta==2),]#interval censored data #negative log likelihood nlogf<function(para) { b0<para[1] b1<para[2] g<para[3] e<sum((b0+b1*data0$x)+g*data0$t+(1/g)*exp(b0+b1*data0$x)*(1exp(g*data0$t))) r<sum((1/g)*exp(b0+b1*data1$x)*(1exp(g*data1$t))) i<sum(log(exp((1/g)*exp(b0+b1*data2$x)*(1exp(g*data2$lower)))exp((1/g)*exp(b0+b1*data2$x)*(1exp(g*data2$upper))))) l<e+r+i return(l) } #likelihood library(maxLik) ll<function(para) { b0<para[1] b1<para[2] g<para[3] e<sum((b0+b1*data0$x)+g*data0$t+(1/g)*exp(b0+b1*data0$x)*(1exp(g*data0$t))) r<sum((1/g)*exp(b0+b1*data1$x)*(1exp(g*data1$t))) i<sum(log(exp((1/g)*exp(b0+b1*data2$x)*(1exp(g*data2$lower)))exp((1/g)*exp(b0+b1*data2$x)*(1exp(g*data2$upper))))) l<e+r+i return(l) } #maximum likelihood est<maxLik(logLik=ll,start=c(para<c(8,0.03,0.0105))) x < list(label=c("beta0","beta1","gamma"),est=c(est$estimate[1],est$estimate[2],est$estimate[3]),low=c(8.1,0.29,0.09),upp=c(7.6,0.31,0.11)) #confidence interval for beta0 plkhci(x,nlogf,"beta0")

ggplot selective labelling and multiplotting error: Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
My goal is to write a function that produces plots with ggplot2 and then use the multiplot function. I want to use a loop and save ggplot objects. I also want to use selective labelling as below :
myplots < list() # new empty list for (j in 2:5){ for (k in 2:5){ p2 < ggplot(total, aes(log(total[j]), log(total[k]))) p1 < p2+ geom_point() + geom_text(data=subset(total, GeneName=='ENSMUSG00000036202.8' GeneName=='ENSMUSG00000086503.1'),aes(x=as.numeric(log(j)),y=as.numeric(log(k)),label=name),colour="#CC0000") myplots[[(length(myplots) +1)]] < p1 # add each plot into plot list print(length(myplots)) } } multiplot(plotlist = myplots, cols = 4)
As a result, I got a plot table. However labels are in the wrong place and I got an error for each plot:
Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.
What can be the reason ? Is there something wrong is my aes selective labelling ? Any help is appreciated.

Remove box and points in legend
How do I remove the the box, ribbon color, and points in the legend? I would just like a straight line representing each color of the
color
. I've tried usingguides()
, but it's not changing.Sample data:
pdat1 < structure(list(type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("10year", "20year", "30year"), class = "factor"), effect = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), value = c(0, 21.89, 27.36, 33.75, 40.57, 47.32, 0, 23, 28.31, 34.96, 42.6, 50.81, 0, 16.9, 22.25, 28.87, 36.4, 44.52, 0, 10.24, 16.8, 24.74, 33.52, 42.55, 0, 10.24, 16.8, 24.74, 33.52, 42.55, 0, 10.24, 16.8, 24.74, 33.52, 42.55), temp = c(0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5), value_max = c(2.91, 19.02, 24.42, 30.88, 37.63, 44.35, 2.9, 20.09, 25.36, 32.05, 39.67, 47.87, 2.97, 14.02, 19.27, 25.89, 33.49, 41.58, 2.42, 7.74, 14.34, 22.27, 31.06, 40.02, 2.45, 7.8, 14.36, 22.26, 31.07, 40.07, 2.46, 7.71, 14.23, 22.23, 31.02, 40.05), value_min = c(2.91, 24.76, 30.3, 36.63, 43.5, 50.3, 2.9, 25.91, 31.27, 37.87, 45.52, 53.75, 2.97, 19.77, 25.24, 31.85, 39.32, 47.46, 2.42, 12.74, 19.26, 27.21, 35.98, 45.08, 2.45, 12.68, 19.24, 27.22, 35.96, 45.02, 2.46, 12.77, 19.37, 27.25, 36.02, 45.05)), class = "data.frame", row.names = c(NA, 36L), .Names = c("type", "effect", "value", "temp", "value_max", "value_min"))
Plot Code
library(ggplot2) ggplot(pdat1) + geom_ribbon(aes(ymax = value_max, ymin = value_min, x = temp, linetype = NA, color = effect, fill = effect), fill = "#C0CCD9", alpha = 0.5 ) + geom_line(aes(x = temp, y = value, color = effect, group = effect)) + geom_point(aes(x = temp, y = value, color = effect), size = 0.5) + ylab("Y") + xlab("X") + guides(color = guide_legend(keywidth = 2, keyheight = 1, override.aes = list(linetype = c(1, 1), size = 1, shape = c(0, 0)))) + facet_wrap(~type)

nonnumeric argument to binary operator error with ggplot
I seem not to be able to fix the "nonnumeric argument to binary operator" error in the following ggplot code in R. Can someone help me?
Pic1 < ggplot(Data1, aes(x = x)+ geom_line(aes(y =y, color = Curves, linetype = Curves), size = 1) + theme(axis.title = element_text(size = 5), plot.title = element_text(size = 7, hjust = .5), legend.text = element_text(size = 7), legend.title = element_text(size = 7)) + labs(x = "Labor", y = "Good", title ="LaborGood"))

Android  TextView at certain position on zoomable ImageView
I am trying to display TextView at certain position over ImageView that can be zoomed in and zoomed out. If part of the image disappears from the screen when zooming in, then text pinned to x and y coordinates from that part of image should disappear as well.
ImageView myImage = (ImageView)findViewById(R.id.myImage); PhotoViewAttacher photoView = new PhotoViewAttacher(myImage); photoView.setMaximumScale(15); //very large zoom photoView.update();
My question is: how to put a TextView on that ImageView on certain posistion?

From known coordinates how to determine what kind of area it is on Google Maps?
For example, I have coordinates (latitude & longitude), I would like to know the location of this coordinates is on a Road, or Park, or Building on Google Maps. Does Google Maps have such API to return this kind of data? Thanks.

OpenGL Unprojecting mouse cursor to find relative position to a certain object?
I have a player object that's moving around the screen. The camera is a fixed camera, looking down on the player (like in Diablo).
Now I want the player object to rotate towards the mouse cursor. The player is not always on the center of the screen (for this case I already have a solution). In order to do this, I think I need to project the mouse cursor to the same height (yaxis) that my player is on (yaxis is "up" in my game) and then check compare player position with cursor position on the same height in world space.
So far, my unprojecting method looks like this:
private bool Unproject(float winX, float winY, float winZ, out Vector3 position) { position = Vector3.Zero; Matrix4 transformMatrix = Matrix4.Invert(World.CurrentWindow.GetViewMatrix() * World.CurrentWindow.GetProjectionMatrix()); Vector4 inVector = new Vector4( (winX  World.CurrentWindow.X) / World.CurrentWindow.Width * 2f  1f, (winY  World.CurrentWindow.Y) / World.CurrentWindow.Height * 2f  1f, 2f * winZ  1f, 1f ); Matrix4 inMatrix = new Matrix4(inVector.X, 0, 0, 0, inVector.Y, 0, 0, 0, inVector.Z, 0, 0, 0, inVector.W, 0, 0, 0); Matrix4 resultMtx = transformMatrix * inMatrix; float[] resultVector = new float[] { resultMtx[0, 0], resultMtx[1, 0], resultMtx[2, 0], resultMtx[3, 0] }; if (resultVector[3] == 0) { return false; } resultVector[3] = 1f / resultVector[3]; position = new Vector3(resultVector[0] * resultVector[3], resultVector[1] * resultVector[3], resultVector[2] * resultVector[3]); return true; }
Now I unproject the mouse cursor once for the near plane (winZ = 0) and the far plane (winZ = 1).
protected Vector3 GetMouseRay(MouseState s) { Vector3 mouseposNear = new Vector3(); Vector3 mouseposFar = new Vector3(); bool near = Unproject(s.X, s.Y, 0f, out mouseposNear); bool far = Unproject(s.X, s.Y, 1f, out mouseposFar); Vector3 finalRay = mouseposFar  mouseposNear; return finalRay; }
My problem is:
How do I know if the values are correct. The values in the "finalRay" Vector are quite small  always. I would have thought that i would get much bigger zvalues because my near plane (perspective projection) is 0.5f and my far plane is 1000f.
And how can I find out if the mouse cursor is left/right (x, +x) or behind/in front of (z, +z) the player? (I know the player's position)
Where is my error?

Python groupby delete row with condition
I'm new on python and I have an issue.
My original dataset is like this one: Original dataset
and I would like to get: final dataset
So the idea is to delete:
 single demand, and
 rows where there is only one different id_user per demand.
I manage to delete single demand with a Counter() but I'm stuck there: probleme here
I don't want to do it manually with index because my proper dataset has more than 30000lines.
Can someone help me with this problem ?

Multiprocessing and interrupting processes after fulfillment of the condition
I have an algorithm where I divide the
big
list into twoelement pieces written in a dictionary, then in the functiondoit
I check if the number3
is in the first place in the given element, if so, I return the whole element, and if not, I returnNone
. Then I wanted to enter a multiprocessing in these divisions and return only the fastest found one. I have this code:import multiprocessing as mul big = [[0,1],[3,0],[1,5],[0,3],[3,1],[0,6],[0,1],[3,2],[1,5],[0,3],[3,3],[0,6],[0,1],[3,4],[1,5],[0,3],[3,5],[0,6],[0,1],[3,6],[1,5],[0,3],[3,7],[0,6],[0,1],[3,8],[1,5],[0,3],[3,9],[0,6],[0,1],[3,10],[1,5],[0,3],[3,11],[0,6]] sources={} for x in range(int(len(big)/2)): sources["d{0}".format(x)] = big[2*x:2*x+2] def doit(L): for i in L: if i[0]==3: return i return None def foo(j): return doit(sources["d{0}".format(j)]) if __name__ == '__main__': pool = mul.Pool(4) for x in pool.map(foo, range(int(len(big)/2))): print (x) if x == None: break pool.close() pool.join()
But I am doing something wrong, because I do not notice the parallelism of processes in the results, because all the time I receive:
[3, 0] None
And I expected that it will not always be a pair of
[3,0]
.How to change it?

Dataframe calculation with condition
I have a dataframe as mentioned below:
DF Count Count2 % A 100 0 0% B 80 20(AB) 20% C 50 30(CC) 37.5% D 25 25(CD) 50%
For that i have used below code:
df1 < data.frame(matrix(nrow = 100)) df2 < data.frame(matrix(nrow = 80)) df3 < data.frame(matrix(nrow = 50)) df4 < data.frame(matrix(nrow = 25)) df < c('A', 'B', 'C', 'D') count < c(nrow(df1), nrow(df2), nrow(df3), nrow(df4)) count2 < vector(mode="numeric", length = 4) for(i in 1:4) { if(i == 1) { count2[i] < 0 } else { count2[i] < count[i1]  count[i] } } percentage < vector(mode="numeric", length = 4) for(i in 1:4) { if(i == 1) { percentage[i] < 0 } else { percentage[i] < ((count[i1]  count[i])/count[i1]) * 100 } } final_df < data.frame(df, count, count2, percentage)
I want to add a new row betwenn B and C (let say it Z) and for that my count2 formula would be (Count2(B)Z) instead of (ZC) and for percentage Count2(Z)/Count2(B).
And Now for C count2 as well as percentage formula would be (BC) instead of (ZC).
Desired output:
DF Count Count2 % A 100 0 0% B 80 20(AB) 20% Z 15 5 (Count2(B)Z) Count2(Z)/Count2(B) C 50 30(CC) 37.5% D 25 25(CD) 50%

R ggmap ggplot2 error “Error: Discrete value supplied to continuous scale”
I am trying to mark airports in India on India map. My code is as follows:
library(ggmap) library(ggplot2) airports < read.csv("C://Users//MEJA03514//Downloads//inairports.csv", header=T) map < get_googlemap("India", zoom = 4) points < ggmap(map) + geom_point(aes(x = longitude_deg, y = latitude_deg), data = airports, alpha = 0.5) points
I downloaded the airports data file from: https://data.humdata.org/dataset/ourairportsind
I am getting an error:
Error: Discrete value supplied to continuous scale
after I am combining
ggmap()
withgeom_point()
function. Can you please help me figure out what is the mistake in this code.Thanks in advance!

How do I view stat parameters available for a geom?
The documentation for
stat_density2d
contains the following example:m + stat_density_2d(aes(fill = ..level..), geom = "polygon")
I meant to replicate the same using
geom_density_2d
for which I assume I need to change the defaultgeom_density_2d(... stat = "density2d")
to something else, but I can't the other options available in the documentation 
In ggmap/ggplot, how to set size in absolute not relative values
In the following simplified example, the size variable controls the relative size of lines to be plotted on the map with
geom_path
.The problem is that apparently the size is not in millimeters (as in documentation for ggplot), but for any two or more values in size variable, the lines with two extremes (min and max) are plotted with minimal and greatest width available and all the other lines are on discrete scale somewhere in the middle.
The greatest width is just too wide and I wanted to make it thinner. But since the size appear to be relative to min/max and is not an absolute (millimeter/pixel whatever) value, I am unable to control the actual size.
Please change the
somevalue
here and see that nothing changes between plots.library(ggmap) base_layer < get_googlemap(center = c(lon = 28.5, lat = 37) , zoom = 3 , maptype="roadmap" , size = c(640,640) , scale = 2 , color = "bw") somevalue < 3 df < data.frame( group = c("g1","g1","g2","g2"), size = c(1,1,somevalue,somevalue), color = c("blue","blue", "red", "red"), lon = c(10,20,10,10), lat = c(52,60,52,60) ) ggmap(base_layer) + geom_path(data = df, aes(x = lon, y = lat, alpha = 0.6, group = group, color = color, size = size))