Adding specific range for y axis to plot - ggplot2

I cannot work out how to apply a specific y axis range to my violin plot
current code is :
library(ggplot2)
X21$`gggnnn`<-as.factor(X21$`gggnnn`)
X21$`RTtype`<-as.factor(X21$`RTtype`)
bp<-ggplot(data=X21,aes(x=RTtype,y=RT,group=RTtype))+
geom_violin(aes(colour=RTtype),outlier.alpha = 1)+
facet_grid(.~gggnnn) +
labs(x="AM or PM", y='Reaction time /ms')+
geom_boxplot(width=0.1,colour="black",alpha=1,outlier.shape=4)+
ggtitle("AM and PM Reaction Time Distributions among Gamers and Non-gamers")+
geom_jitter(data=X21,aes(x=RTtype,y=RT,group=RTtype, colour=RTtype,shape=gggnnn))+
bp
this gives the plot as shown:
[1]:https://i.stack.imgur.com/3Aggm.png
I then tried to set y axis limits with adding a '+ylim(150,900)' , however this just truncated my data:
[2]:https://i.stack.imgur.com/kDiRs.png
I now see that this is a limit on the range of the data, not the values on the axis (i am looking for the y axis to go from 150 to 900, also i do not know how to change the y axis grid spacing, as it is currently in intervals of 250, which is harder to interperit, i would like to set this to 100.
I attempted to do this with '+scale_y_continuous(breaks = seq(150,900, by = 100))', However it had no effect on the plot
Any help would be much appreciated
datasheet format:
[3]: https://i.stack.imgur.com/iXKXF.png

in order to set y coordinate limits use
coord_cartesian(ylim = c(100,900))
generalised this is:
coordinates_cartesian(xlim = c(lower limit,upper limit), ylim( c =(lower,upper)
in order to set spacing use
scale_y_continuous(breaks = seq(100, 900, by = 200))

Related

Matplotlib - x axis does not match the data

The description of the data frame
When I try to find the relationship between budget and revenue_of_investment
x = dfm_2.budget
y = dfm_2.revenue_of_investment
plt.figure(figsize = (10,8))
plt.xlim((40000,42500000))
plt.scatter(x,y)
The output is:
I know the range of the budget is big, but I do not figure out the data on the x-axis.
I even set the range, however, the x-axis doesn't fit the data.
If I understand your question correctly (i.e. that the plot is not displaying all of the data on the x-axis), it's because your upper xlim is too small.
The maximum value of dfm_2.budget is 4.25 * 1e8 (i.e. 425000000), but your xlim upper limit is set to 4.25 * 1e7 (i.e. 42500000) (i.e. you're missing a zero in your plt.xlim())

How can I make a scatter Plot with what I have?

total_income_language = pd.DataFrame(df.groupby('movie_facebook_likes')['gross'].sum())
average_income_language = pd.DataFrame(df.groupby('movie_facebook_likes')['gross'].mean())
d = {'mean':'Average Income','sum':'Total Income'}
df1 = df.groupby('movie_facebook_likes')['gross'].agg(['sum','mean']).rename(columns=d)
ax = df1.plot.bar()
ax.set(xlabel='Facebook Likes', ylabel='Dollar Values(Gross)')
So, the code I have above does a good job ploting a bargraph. But When I tried to make it into a scatter plot by changing the .bar() to .scatter() It gives me the following error:
What do I need to fix to make it a scatter plot?
Expected Output:
As the error tells you, you need to specify x and y arguments which represent the coordinates of your points.
For example (assuming that there are movie_facebook_likes and gross columns in your DataFrame):
ax = df1.plot.scatter(x='movie_facebook_likes', y='gross')
See the documentation:
x, y : label or position, optional
Coordinates for each point.

Annotation box does not appear in matplotlib

The planned annotation box does not appear on my plot, however, I've tried a wide range of values for its coordinates.
What's wrong with that?!
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def f(s,t):
a = 0.7
b = 0.8
Iext= 0.5
tau = 12.5
v = s[0]
w = s[1]
dndt = v - np.power(v,3)/3 - w + Iext
dwdt = (v + a - b * w)/tau
return [dndt, dwdt]
t = np.linspace(0,200)
s0=[1,1]
s = odeint(f,s0,t)
plt.plot(t,s[:,0],'b-', linewidth=1.0)
plt.xlabel(r"$t(sec.)$")
plt.ylabel(r"$V (volt)$")
plt.legend([r"$V$"])
annotation_string = r"$I_{ext}=0.5$"
plt.text(15, 60, annotation_string, bbox=dict(facecolor='red', alpha=0.5))
plt.show()
The coordinates to plt.text are data coordinates by default. This means in order to be present in the plot they should not exceed the data limits of your plot (here, ~0..200 in x direction, ~-2..2 in y direction).
Something like plt.text(10,1.8) should work.
The problem with that is that once the data limits change (because you plot something different or add another plot) the text item will be at a different position inside the canvas.
If this is undesired, you can specify the text in axes coordinates (ranging from 0 to 1 in both directions). In order to place the text always in the top left corner of the axes, independent on what you plot there, you can use e.g.
plt.text(0.03,0.97, annotation_string, bbox=dict(facecolor='red', alpha=0.5),
transform=plt.gca().transAxes, va = "top", ha="left")
Here the transform keyword tells the text to use Axes coordinates, and va = "top", ha="left" means, that the top left corner of the text should be the anchor point.
The annotation is appearing far above your plot because you have given a 'y' coordinate of 60, whereas your plot ends at '2' (upwards).
Change the second argument here:
plt.text(15, 60, annotation_string, bbox=dict(facecolor='red', alpha=0.5))
It needs to be <=2 to show up on the plot itself. You may also want to change the x coorinate (from 15 to something less), so that it doesn't obscure your lines.
e.g.
plt.text(5, 1.5, annotation_string, bbox=dict(facecolor='red', alpha=0.5))
Don't be alarmed by my (5,1.5) suggestion, I would then add the following line to the top of your script (beneath your imports):
rcParams['legend.loc'] = 'best'
This will choose a 'best fit' for your legend; in this case, top left (just above your annotation). Both look quite neat then, your choice though :)

Excel VBA: change x axis value as desired

I am creating charts with origin(100,0) i.e, x axis value=100. At first I used,
ActiveChart.Axes(xlValue).MajorUnit = 10
and I got the x axis ... 70,80,90,100,110,120,130....etc
I thought it would be better if I had axis values as ...60,80,100,120,140... etc. So I edited my code as follows,
ActiveChart.Axes(xlValue).MajorUnit = 20
But now I am getting ...70,90,110,130... etc.
How can I get my x axis as ...60,80,100,120,140...?

How multiple plots can be compared

I have two plots. and i have to compare these plots by making one plot.
example
plot1 x range (0 - 50), some y-range
plot2 x range (50 - 100), some y-range
resultant plot x range (0 -100)
How this can be done?
Am I missing something or can you just do:
plot(x1,y1);
points(x2-50,y2);
More generally, you can plot any 2 things together. You have to make the x and y scales constant across both plots. Something like this should work:
# assume I have vars: x1 and x2 and y1 and y2
# figure out the range of all y vars
yRange = range(y1,y2)
xRange = range(x1,x2)
plot(xRange,yRange, type='n') #creates the axes but doesn't put any points on them
points(x1,y1)
points(x2,y2, col='red')
My apologies, I thought this was in the R group and answered based on that.